MacBook Air/Pro: run script upon lid close?

Solution 1:

There does not seem to be a way for a program to get notified when the lid is closed. However, the state of the lid can be monitored periodically (and in this particular case, every second) and actions taken based on the lid state.

I do not know of any program that can provide a complete solution for your need, but you can use the following information to build a solution yourself.

  1. Download this clamshellstate.pl perl script
  2. You can run it in Terminal (under /Applications/Utilities) like:

    ./clamshellstate.pl 1  
    

    to get an output like "Open" or "Closed" indicating the current lid state

  3. Create a shell script like (substitute script-to-run with a shell script you'd like to run):

    ./clamshellstate.pl 1 | grep Closed > /dev/null && *script-to-run*
    
  4. Configure this script to run once every second using launchd.plist. Or use Lingon to easily configure launchd.plist.

Note: Instead of clamshellstate.pl, you can also use the following command in Terminal:

ioreg -r -k AppleClamshellState | grep '"AppleClamshellState" = Yes' | cut -f2 -d"="

to get a "Yes" for the closed state and a "No" for the open state.

Solution 2:

You could also try using SleepWatcher. See the readme for instructions:

1. Install the SleepWatcher software:

$ sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8
$ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher /usr/local/sbin
$ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8

2. Read the man page and think about the features of SleepWatcher you want to use:

$ man sleepwatcher

3. Write small shell scripts that perform the actions you want to trigger by SleepWacher and test them.

4. Test your scripts in combination with SleepWatcher, started in the foreground in a Terminal window, e. g.:

$ /usr/local/sbin/sleepwatcher --verbose --sleep /path/to/your/sleepscript --wakeup /path/to/your/wakeupscript ...

5. Put your SleepWatcher command line into a launchd agent configuration file. As an example you can use one of the plists from ~/Desktop/sleepwatcher_2.2/config. For more information about launchd and its configuration, see the man pages for launchd and launchd.plist and http://developer.apple.com/macosx/launchd.html.

6. Copy your launchd agent configuration to /Library/LaunchDaemons or ~/Library/LaunchAgents, depending on whether you need a system wide daemon or a user agent.

7. Load the launchd agent configuration using launchctl (see examples above and the launchctl man page).

Solution 3:

You could try using the ControlPlane app which can act on various state changes on your Mac including whether the lid is closed or not.

Also it is possible to use Hammerspoon to activate specific scripts on waking and sleeping as mentioned in my answer to another question. The lua scripts could be augmented to check for the lid state using the ioreg -r -k AppleClamshellState test mentioned in M K's answer here.