PDA

View Full Version : Quick cron question: closing files




Blue Velvet
Apr 23, 2005, 04:24 AM
Thanks to SuperBovine I have downloaded & had a play with Cronnix.

For more details on why I need something like it see here.
http://forums.macrumors.com/showthread.php?t=121728

Although I can get it to open files on schedule, I can't get it to close them.
I've tried a number of commands gleaned from various Unix command webpages but I'm almost out of my depth here.

Any tips?

BTW: This seems to open them fine at 10:10am every day.
10 10 * * * /usr/bin/open"/Users/bluevelvet/Desktop/test.pdf"



robbieduncan
Apr 23, 2005, 04:51 AM
AppleScript? Obviously you can close files from AppleScript pretty easily (as long as you know which app has opened them). You can run an AppleScript from the command line via osascript. So create a file containing the AppleScript you want to run (just a plain text file, create it in TextEdit or vi). You then just call osascript <filename>.

Blue Velvet
Apr 23, 2005, 04:56 AM
Problem is, is that this has to be idiot-proof including for me...

robbieduncan
Apr 23, 2005, 05:02 AM
OK lets take this one step at a time.

We want to create an AppleScript to close a specific file in a specific app (we'll deal with the timing in cron). If we can guarantee that the app only has 1 file open (the one we are interested in closing) we can make this simpler by simply quiting the app! As this is simpler that's what I'll do here (if you really need to kill a specific file post again and I'll do that instead).

So open TextEdit and put this in a new file:

tell application "Preview" to quit

and save the file. Note that Preview can be changed for the name of the correct app :)

Then add 20 10 * * * osascript <your_file_name> to cron. This will quit the app at 10:20 am every day.

Blue Velvet
Apr 23, 2005, 05:08 AM
OK lets take this one step at a time.

We want to create an AppleScript to close a specific file in a specific app (we'll deal with the timing in cron). If we can guarantee that the app only has 1 file open (the one we are interested in closing) we can make this simpler by simply quiting the app! As this is simpler that's what I'll do here (if you really need to kill a specific file post again and I'll do that instead).

So open TextEdit and put this in a new file:

tell application "Preview" to quit

and save the file. Note that Preview can be changed for the name of the correct app :)

Then add 20 10 * * * osascript <your_file_name> to cron. This will quit the app at 10:20 am every day.


Very helpful but I will need to close a specific file but leave the app running in order to start up another file.

Thanks for all advice so far, I really appreciate it and will be spending most of the afternoon messing with this to get it right before rolling it out at work.

robbieduncan
Apr 23, 2005, 05:12 AM
Whilst the app would obviously start up again to open the next file I appreciate this could take some time to actually complete. I have just tested this in TextEdit and it worked for me:


tell application "TextEdit"
close document "NSCoreSuite.sdef"
end tell


You will probably want to change the document and app names :)

Blue Velvet
Apr 23, 2005, 05:14 AM
Thanks. I'll have a play and get back to you...

Big-up -- robbieduncan!

robbieduncan
Apr 23, 2005, 05:28 AM
I thought I'd post this as I have realised that you said you were going to open another doc in the same app. There is no need to go back to cron to do this. You can do it in the same AppleScript that you use to close the previous document.


tell application "TextEdit"
try
close document "NSTextSuite.sdef"
end try
try
open "Macintosh HD:Users:robbie:Documents:LineCounts.rtf"
end try
end tell


Note that I have added the try/end try blocks to stop any errors getting thrown on screen if, for example, the open file is not open, or the next file cannot be found.

Also note that AppleScript does not use standard Unix style paths. You need to start with the disk (not /) and directories are separated with :

Glad to be of help :)

Blue Velvet
Apr 23, 2005, 08:34 AM
Glad to be of help :)

Ok, after an hour or so of messing around with lots of different files and scripts (complete Terminal & Applescript novice) I've got it working perfectly and more importantly, understand what I'm doing. After all, I'm just a lowly hack designer...

You are a champ! :D