Eject Time Machine on sleep (or any other disk)

In the morning, my Macbook is usually asleep - and plugged into the hub that connects it to the Time Machine disk. I usually just grab it and go - leading to the dreaded 'disconnect' message when I wake it up at work.

Now, I have a solution - to eject the disk when my Mac goes to sleep. You'll need SleepWatcher, a cool little program from Bernhard Baehr. It is compatible with Leopard, but appears to run fine on my Snow Leopard system. Install SleepWatcher and the startup item.

Next, you'll need to create the script. You can do this one of two ways: edit /etc/rc.sleep (as root) or create a file in your home director called .sleep. The file can contain any commands you want to run when the system goes to sleep. (/etc/rc.wakeup and ~/.wakeup, have the obvious companion effect) To eject a disk, a simple script command works:

osascript -e 'tell application "Finder" to eject "Backups"'

where "Backups" is the label of the disk you wish to eject.

If you get messages about 'no loadable handlers/suitable images/etc' from other programs, especially Adobe, try this modification:


arch -i386 osascript -e 'tell application "Finder" to eject"Backups"'

Thanks to this tip from MacOSXTips for the idea.

[Update]As a useful companion piece, I want my Time Machine volume to reconnect when I wake the computer at home. Thanks to this tip, also from MacOSXTips, you can automatically find the device number and mount the volume.

#!/bin/bash
backupsVolume=`diskutil list | awk '/ Backups / {print $6}'`
if [ ! -z "$backupsVolume" ]
then
diskutil mount $backupsVolume
fi

where "Backups" is the title of the volume. If you aren't sure, run "diskutil list" from the console to see. Include the leading and trailing spaces in the awk statement for the most exact match. Put this in ~/.wakeup to have it run by SleepWatcher.

,

Powered by Drupal, an open source content management system