6 Replies Latest reply: Feb 7, 2013 7:37 AM by MrHoffman
brendafromdidsbury Level 1 (25 points)

First off, with apologies, I am not a script familar person at all.  I have an elderly friend who is switching to Mac for the first time.  I wanted a way to close all open programs, with a prompt to save any open files and then run a Time Machine backup and shut down - preferably without restarting next day with any windows opening automatically.  Time Machine is set to run every hour but she's a writer and could lose work if Time Machine doesn't run before shutting down for the night.  So I searched and found some small scripts that I tried to put together.  I think it works but after Time Machine runs it waits a very long time before shutting down.  I'm wondering if some of you kind folks could refine this for me.  (I do realize that automator has a quit all programs that I can save as an app but I'm trying to make this a one step 'app' process.)


So what I have is this:


tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}


          tell application "Finder"

                    set process_list to the name of every process whose visible is true

          end tell

          repeat with i from 1 to (number of items in process_list)

                    set this_process to item i of the process_list

                    if this_process is not in white_list then

                              tell application this_process


                              end tell

                    end if

          end repeat

on error

          tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0

end try

do shell script "/bin/bash -c '/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper > /dev/null 2>&1  &'"


  delay 5

          if not IsProcRunning("backupd") then

                    ignoring application responses

                              tell application "Finder" to shut down

                    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



.... frankly I don't understand most of this in reality but it's a need I'd like to help her with on her Mac Mini. 



Many Thanks,



MacBook Air, Mac OS X (10.7), 13" Intel top MBAi model & Iphone 4
  • kikashi Level 1 (10 points)

    I can't help wondering if a real-time online backup such as Dropbox wouldn't be a much better and easier solution to the specific problem. All your friend would need to be told is "after you save for the last time each day, don't shut down until the little icon in the menu bar has a tick next to it".




    PS: I live in Didsbury as well, but it's the one south of Manchester, England

  • twtwtw Level 5 (4,910 points)

    First off, you're overthinking this.  There's no reason why your friend would lose any data by not backing up prior to shutdown (unless she has a flaky hard drive that needs to be replaced).  Assuming she's saving her work when she quits her apps, she should be fine.


    Second, trying to auto-quit applications is dangerous.  You don't check to see if any files need to be saved (there's no easy way to make that check anyway), and that in itself could cause data loss.


    Third, time machine has its own command line utility: tmutil.  There's no reason to script backupd.


    Last, if I remember correctly, whether or not apps and windows reopen at startup can be set on a system-wide or per-application basis.


    Give a clearer and more specific description of the goals you're trying to accomplish: what kind of apps are likely to be open, which of them will/might need to save data, what you want the machine to look like on restart, all with details.  That would help a lot.

  • MrHoffman Level 6 (13,975 points)

    If it's an option with the OS X version and hardware in use here, enable and use Power Nap, and have her sleep the plugged-in laptop.  Avoids most of the problems here.

  • rccharles Level 5 (7,080 points)

    Second, trying to auto-quit applications is dangerous.  You don't check to see if any files need to be saved (there's no easy way to make that check anyway), and that in itself could cause data loss.

    She is quitting at the Application level.  The applications will give a warning message & ask you to save.


    Here is what TextWranger did:

    Picture 7.png

  • twtwtw Level 5 (4,910 points)

    true enough, but application closure is handled asynchronously, as is the shell script of backupd in the next block, and the shutdown command in the block after is wrapped in a repeat loop that's going to freeze the script and the Finder solidly.  Ugly race condition in an unstable system state: all bets off.

  • MrHoffman Level 6 (13,975 points)

    I've had success using Mission Control — something experienced users can want to avoid — and particularly with using sleep rather than shutdown, using Power Nap where that's available, of guiding folks into the use of Automatic Termination and Automatic Save, and just letting Time Machine "do its thing". 


    Consider why are you closing applications.  Why are you shutting down.  You're clearly very familiar with computers and particularly with computer operations, and you're clearly applying that knowledge here.   Have you tested operations with automatic save and related features enabled, with sleep and (if you have it) Power Nap, and determined whether you're getting (enough) good backups out of the configuration?


    Most of the "fun" I have with these cases tends to be with WiFi issues and ISP network glitches, which can lead to those blasted password dialog prompts in Mail.app, and with the difficulties and unfamiliarity with the keychain.  But with backups?  Those — barring network errors or Time Capsule errors — generally "just work".