Fun with SleepWatcher

I do the majority of my computing work on a MacBook Pro — a laptop computer. The primary reason for a laptop is portability. Sure most of my work gets done at my desk, but sometimes I have to move or want to move. But moving isn’t always so quick and easy because I have lots of things hooked up to the MacBook Pro, like an external FireWire hard drive so I can run Time Machine backups. Trouble is, you can’t just yank out the cable for the disk, you must eject it, then you can remove the cable, else you could risk corruption of the data on the drive. Of course when you’re in a hurry to get out, or just consider we all get absent-minded from time to time gee… this is a pain in the neck and wouldn’t it be nice if we could automate it so we didn’t have to worry about it! Isn’t that what computers are useful for?

As I searched about for a solution, I found a little app called autoEJECT. It’s not bad, but after using it for a few days it just didn’t work like I needed it to. It did unmount things fine, but it failed to mount my FireWire drive. It has an option to “Force Mount”, which I then enabled but when I did so it mounted everything including the secret hidden partitions Apple has for recovery and booting. This is not desired as that 1. clutters things up, 2. exposes volumes that you do NOT want to risk to change. Even then it was a little quirky in how it would remember volumes to mount and unmount and just got in the way more than it was being useful. I figured there had to be a better way.

I found something called SleepWatcher.

SleepWatcher is a system daemon that watches for various events on the system, like sleep and wake, and allows you to execute scripts in response to those events. The app isn’t for novices, and the author actually goes out of his way to make it a bit complicated to use because it’s not a tool for the masses to use. But if you know what you’re doing, it can be useful. So I figured to give it a shot.

Of course, SleepWatcher itself isn’t what does the voodoo. You need scripts.

This website listed a couple scripts.

His sleep script was:

#!/bin/sh
osascript -e 'tell application "Finder" to eject disks'

and his wake script was:

#!/bin/sh
/usr/sbin/diskutil list | \
 awk '/Apple_HFS/ {print $NF}' | \
 xargs -I{} /usr/sbin/diskutil mount {}

Those are reasonable starting scripts. I thought about changing the wake script to just look for my one volume, because as it is now is ends up mounting all my partitions. Since they’re already mounted it really makes no difference, but it’d just be more efficient to scan for my lone volume. Still tho, this is useful for being complete.

The sleep script I modified slightly based upon what I found here. The change I made was simple:

#!/bin/sh
osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)'

which works a bit better in my situation. Granted, this will kick out optical discs, but as I rarely use them it’s not a big deal to me. The author also notes how this will not work if the Finder isn’t running, but I use the Finder and it should always be running (else there’s a problem).

Along the same lines as these is this hint from Mac OS X Hints, but he takes some vastly different approaches in what is selected for unmounting and what is selected for mounting. Not exactly what I want, but it shows what else you can do.

Anyways, SleepWatcher is pretty nifty and reading the code seems to be pretty well written too. Cheers, Bernhard Baeher!

Join the discussion!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.