I am using the following Applescript to run a Time Machine backup, and then shut down OS X on the computer at our store.

do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper"

    delay 10
    if not IsProcRunning("backupd-helper") then
        ignoring application responses
            tell application "loginwindow" to «event aevtshut»
        end ignoring
        exit repeat
    end if
end repeat

on IsProcRunning(theProc)
        do shell script "ps auxc | grep \"" & theProc & "\""
        return true
    on error
        return false
    end try
end IsProcRunning

It works fine and shuts down the computer unless another user is logged in, then the Security Agent window pops up asking for an administrator username and password.

I'm curious if someone can help me add to this script the ability to check if the Security Agent window is open, and if it is, to enter the username and password in their relative fields?


If you can go to sleep instead of shutting down, it wouldn't require administrator privileges or confirmation from other logged in users:

do shell script "tmutil startbackup -b"
tell application "System Events" to sleep

You could also use shutdown (it ejects disks properly and so on, but you could lose some unsaved changes):

do shell script "tmutil startbackup -b && shutdown -h now" with administrator privileges

A shell function that could be added to ~/.profile or ~/.bash_profile:

tmshut() { sudo -s -- 'tmutil startbackup -b && shutdown -h now'; }
  • I appreciate the answer, but I'm a little confused on how to implement it. First off, are you suggesting that I just put the computer to sleep instead of shutting it down? If I use your second idea, of using shutdown, how would I work that into my existing code? Or would I just replace my code with yours? – stefmikhail Oct 15 '11 at 21:32
  • I edited the answer. tmutil -b basically replaces the code posted in the question. (tmutil = Time Machine utility, added in 10.7; -b = block, or wait until the backup has finished.) – Lri Oct 16 '11 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.