I’m working on a script that will help complete this daily backup task:
With CronniX, we run an applescript that mounts a network drive shortly before a backup program (in this case SilverKeeper) is set to start up and do its thing. That all works fine. After the backup program begins, CronniX runs another Applescript that, ideally, (and this is where I need som help/advice) will:
- Check every 5 minutes if SilverKeeper is still running.
1a. If SK is running, wait another 5 minutes and check again.
1b. If SK is not running (i.e., it is done backing up) we’d like the script to
display a dialog saying “Mac will shut down in X seconds” with option to cancel, give up after X seconds (if no user response), loop through all open applications and close them, saving any open files (in case someone forgot to close Photoshop or something), and then proceed to shut down.
My friend and I got pretty far with this: we can check every X minutes, and if our target app is no longer open, we can proceed with shutdown. My main questions (where this is failing) are:
What is the best way to ‘wait X minutes’? Delay? with timeout?
When going for the shutdown, we seem to be looping through all the processes, not open apps. Script breaks when the Finder quits itself! Also, is there a way to reference an app as a variable? Because we can’t get any flavor of ‘quit “TextEdit” saving yes’ to work if we aren’t explicitly referencing a quoted string.
Anyway, that sure was a mouthful for a first post. I found that I didn’t have our final draft script with me here at home…but I should be able to update this post tomorrow with the more complete script (showing the quit programs loop). For now, though, the following should give you a good idea of where we are at with this:
set app_run to true
repeat until app_run is equal to false
with timeout of 60 seconds
tell application "System Events"
get name of every process -- retrieves name of all running programs
if (name of processes) contains "SilverKeeper" then
-- we want it to loop and start over, checking again in x minutes
--return "yes"
set app_run to true
tell application "Finder"
activate
end tell
say "Backup still in progress" -- only here for testing
else
set app_run to false
display dialog "The computer is about to shutdown in 15 secs." buttons {"Ok", "Cancel"} default button 1 giving up after 15
(* would love to have a loop here that loops through all open apps and saves any open documents before closing the app *)
tell application "Finder" to shut down
exit repeat
end if
delay 5
end tell
end timeout
end repeat
Thank you for any thoughts on this!