Scripting CronniX task

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:

  1. 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!

Here’s the handler that I use in a daily backup script that launches Retrospect. Retrospect has a setting that allows it to quit after completing a backup so the handler checks to see if Retro has quit and then puts the computer to sleep.

to sleep_()
	set exists_ to true
	repeat until exists_ is false
		do shell script "sleep 60"
		tell application "System Events" to ¬
			set exists_ to exists process "Retrospect Express"
	end repeat
	tell application "Finder" to sleep
end sleep_

As you can see, there are two varieties of sleep in the script. The first is similar to AppleScript’s delay and it is measured in seconds. A member of the AppleScript dev team has stated that it demands less of the CPU than delay. The Finder sleep simply sleeps the computer/monitor (Energy Saver style).

I don’t know of a way to have a script shut down apps in a friendly way unless each app is scriptable to the extent that it has a terminology to save or close without saving documents.

– Rob

Thanks, Rob. We don’t NEED to incorporate the friendly shutdown with save, but we were just trying to add that because we love our fellow co-workers so much :twisted:

I gather that would only work for programs that are scriptable, anyway.