keep inkjet nozzles from clogging

I’m trying to exercise my inkjet printer every few days to keep the nozzles flowing. I’m using Apple Calendar events to schedule a printout of a specific graphic at set intervals, and that’s working well. The graphic is stored as an alias at Pictures/testPrint.jpg. Now I’d like to change it so that a different graphic is used for each printout, making a random selection from a folder of jpgs at Pictures/PrintTest/, but I’d like to keep Calendar as my scheduler because it’s working so well.

Here’s what I’d like to happen. I’ll add a new Calendar event that fires a few minutes before the actual printout event is expected, and this will fire the new script I’m asking for.

The new script (saved as an app) will choose a new jpg at random from Pictures/PrintTest/, then create an alias for this jpg, rename it testPrint.jpg, and move it to Pictures/testPrint.jpg, replacing the existing testPrint.jpg.

Then the original Calendar event fires and the printout happens as normal, except it will be a different graphic each time.

I’d appreciate some help with this.

You may try to use :

set PicturesFolder to path to pictures folder as text
set theSource to PicturesFolder & "PrintTest"
tell application "System Events"
	set thePictures to files of folder theSource
	set aPicture to path of some item of thePictures
	--duplicate file aPicture to folder PicturesFolder with properties {name:"testPrint.jpg"} -- would issue error # -1717
end tell
do shell script "cp " & POSIX path of aPicture & space & POSIX path of (PicturesFolder & "testPrint.jpg")

Alternate code using ASObjC (my preferred one).

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set PicturesFolder to path to pictures folder as text
set theSource to PicturesFolder & "PrintTest"

set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theSource)
set fileManager to current application's NSFileManager's |defaultManager|()
set {theURLs, theError} to fileManager's contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)
set theFiles to (theURLs as list)
set theSourceURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of some item of theFiles)
set theDestURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of (PicturesFolder & "testPrint.jpg"))
set theFileManager to current application's NSFileManager's |defaultManager|()
try -- to delete the dest if it already exists
	theFileManager's removeItemAtURL:theDestURL |error|:(reference)
end try
theFileManager's copyItemAtURL:theSourceURL toURL:theDestURL |error|:(reference)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 1 mars 2019 14:07:16

Thank-you Yvan. Simple and elegant as always. And it works great.

[Edit]
Actually, I replied to this before scrolling down to find your second script. Your first script does everything I want, so can I ask what are the benefits of the second script that cause you prefer it? Thanks.

The second script is supposed to be faster than the first one.

Here is a draft which is supposed to print “some” file in a single code.
No need to store a replicate of “some” file.

(*
https://macscripter.net/viewtopic.php?id=46761

Print some file

*)

on run
	my Germaine()
end run

on Germaine()
	set theSource to (path to pictures folder as text) & "PrintTest"
	
	tell application "System Events"
		set thePictures to files of folder theSource
		set aPicture to (some item of thePictures) as «class furl»
	end tell
	
	tell application "Preview"
		print aPicture without print dialog
	end tell
end Germaine

The same using ASObjC would be :

(*
https://macscripter.net/viewtopic.php?id=46761

Print some file ASObjC

*)


use AppleScript version "2.4"
use framework "Foundation"
use scripting additions


on run
	my Germaine()
end run

on Germaine()
	set theSource to (path to pictures folder as text) & "PrintTest"
	
	set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theSource)
	set fileManager to current application's NSFileManager's |defaultManager|()
	set {theURLs, theError} to fileManager's contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)
	set theFiles to (theURLs as list)
	
	tell application "Preview"
		print (some item of theFiles) without print dialog
	end tell
	
end Germaine

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 2 mars 2019 13:56:49

If you really need to keep two steps, it may be a good idea to create a symbolic link as a substitute of the copy.

It may be achieved with :

(*
https://macscripter.net/viewtopic.php?id=46761

Copy some file ASObjC

*)


use AppleScript version "2.4"
use framework "Foundation"
use scripting additions


on run
	my Germaine()
end run

on Germaine()
	set PicturesFolder to path to pictures folder as text
	set theSource to PicturesFolder & "PrintTest"
	
	set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theSource)
	set fileManager to current application's NSFileManager's |defaultManager|()
	set {theURLs, theError} to fileManager's contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)
	(*
	# first version
	set theFiles to (theURLs as list)
	set theSourceURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of some item of theFiles)
*)
	# alternate version
	set indx to random number from 0 to ((count theURLs) - 1)
	set theSourceURL to theURLs's objectAtIndex:indx
	
	set theDestURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of (PicturesFolder & "testPrint.jpg"))
	
	if (theURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean then -- to delete the dest if it already exists
		fileManager's removeItemAtURL:theDestURL |error|:(reference)
	end if
	fileManager's createSymbolicLinkAtURL:theDestURL withDestinationURL:theSourceURL |error|:(missing value)
end Germaine

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 3 mars 2019 19:11:46