Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blue Velvet

Moderator emeritus
Original poster
Jul 4, 2004
21,929
265
Thanks to SuperBovine I have downloaded & had a play with Cronnix.

For more details on why I need something like it see here.
https://forums.macrumors.com/threads/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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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>.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

Moderator emeritus
Original poster
Jul 4, 2004
21,929
265
robbieduncan said:
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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:

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

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

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.

Code:
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

Moderator emeritus
Original poster
Jul 4, 2004
21,929
265
robbieduncan said:
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.