10.5: Eject drives and run iSync before sleep via script

Apr 25, '08 07:30:00AM

Contributed by: avn

This is a little AppleScript that I wrote to check and ask if the machine should unmount peripheral disks, and optionally perform an iSync, before sleeping. I was motivated to write this after about the fifteenth time I woke my laptop up after taking it home from work only to be greeted with a warning that I did not properly disconnect my Time Machine Disk.

Additionally, since I use iSync to sync my date book and address book with my Nokia cell phone, I thought it would be a good idea to have it auto-sync before I put the machine to sleep. If you are capable with AppleScript, you can go in and make modifications for your system (e.g. turn off iSync, or the options to unmount). One caveat: if your home disk is not the same as the startup disk (unlikely on a laptop), the script will ask if you would like to unmount that disk before sleeping.

I packed the script as an application bundle (with a 10.5-sized icon) for optional placement directly into the dock -- download it from macosxhints.com [464KB download]. Here's the source:

-- A few commands of interest before sleeping
-- AVN   Mon Apr 21 16:04:40 EDT 2008
-- Run iSync to get latest information on phone 

-- Unmount the backup disk if it is currently mounted.  
-- This should really be done with all external disks
tell application "Finder"
  display dialog "Preparing to sleep.  This will attempt to eject the BackupDisk, and run iSync before sleeping. " with title "mySleep?"
  set SDstring to name of startup disk as string -- get startup disk name  
  set AttachedDisks to name of disks -- get list of attached disks
  set DiskCount to number of disks -- skip repeat if only 1 disk
  if DiskCount is not equal to 1 then
    repeat with CurrentDisk in AttachedDisks -- run on all disks
      set CDstring to CurrentDisk as string -- create stringname for comparison
      if CDstring is not equal to SDstring then --if not the startupdisk
        try
          display dialog ({"Unmount ", CDstring, "?"} as string) with title "mySleep: eject disk?"
          eject disk CurrentDisk
        end try
      else
        display alert ({CDstring, " is being skipped"} as string) giving up after 2
      end if
    end repeat
  end if
  
  set dosync to display dialog "Please disconnect power and all unmounted peripherals before continuing." buttons {"Cancel", "Sleep now", "Sync then Sleep"} default button "Sync then Sleep" cancel button "Cancel" with title "mySleep: disconnect peripherals"
  if button returned of dosync is "Sync then Sleep" then
    tell application "iSync"
      activate
      tell application "System Events" to set visible of process "iSync" to false
      synchronize
      repeat
        if not syncing then exit repeat
      end repeat
      quit
    end tell
  end if
  
  tell application "Finder"
    display dialog "Good night" default button "Ok" giving up after 2 with title "mySleep"
    sleep
  end tell
end tell
[robg adds: I haven't tested this one, but I did download the disk image and confirm that it's just the script rolled up in an application bundle.]

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080423101340268