1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Quick cron question: closing files

Discussion in 'Mac Programming' started by Blue Velvet, Apr 23, 2005.

  1. Moderator emeritus

    Thanks to SuperBovine I have downloaded & had a play with Cronnix.

    For more details on why I need something like it see here.

    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"
  2. Moderator


    Staff Member

    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>.
  3. Moderator emeritus

    Problem is, is that this has to be idiot-proof including for me...
  4. Moderator


    Staff Member

    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.
  5. Moderator emeritus


    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.
  6. Moderator


    Staff Member

    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 :)
  7. Moderator emeritus

    Thanks. I'll have a play and get back to you...

    Big-up -- robbieduncan!
  8. Moderator


    Staff Member

    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"
    		close document "NSTextSuite.sdef"
    	end 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 :)
  9. Moderator emeritus

    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

Share This Page