Ever open your laptop in a public place and have it make all sorts of unanticipated racket? Me too. I recently got a MacBook Air (verdict: it's everything I always wanted my 12” PowerBook to be) and decided it needed to mute itself when going to sleep. After waking, if and when I want sound again, it's easy enough to tap one of the volume-related keys. Certainly easier than going back in time.

I expected OS X to have scripts that run on various power-related events (that's what its cousin NetBSD does) and I just needed to find and tweak them. That may be the case, but I didn't turn anything up. What I did find was a small third-party utility called sleepwatcher. From its man page:

sleepwatcher is a program that monitors sleep, wakeup and idleness of a Mac. It can be used to execute a Unix command when the Mac or the display of the Mac goes to sleep mode or wakes up or after a given time without user interaction. It also can send the Mac to sleep mode or retrieve the time since last user activity.

With the default install, all I had to do was wrap an AppleScript call in an executable $HOME/.sleep:

#!/bin/sh
osascript -e 'tell application "System Events" to set volume with output muted'

That's it. Of course, I'm still pretty good at embarrassing myself in other ways.