Wednesday, March 20, 2019

#1 2019-03-01 01:49:21 am

xltrader100
Member
Registered: 2008-12-24
Posts: 34

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.

Offline

 

#2 2019-03-01 07:07:34 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3367

Re: keep inkjet nozzles from clogging

You may try to use :

Applescript:

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).

Applescript:

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

Last edited by Yvan Koenig (2019-03-01 09:43:09 am)

Offline

 

#3 2019-03-01 11:20:35 am

xltrader100
Member
Registered: 2008-12-24
Posts: 34

Re: keep inkjet nozzles from clogging

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.

Last edited by xltrader100 (2019-03-01 01:18:12 pm)

Offline

 

#4 2019-03-02 07:07:13 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3367

Re: keep inkjet nozzles from clogging

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.

Applescript:

(*
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 :

Applescript:

(*
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

Offline

 

#5 2019-03-03 12:13:00 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3367

Re: keep inkjet nozzles from clogging

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 :

Applescript:

(*
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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)