Skip to main content

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

  1. 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*
    
  2. 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'AppleClamshellState | cut -f2 -d"="

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

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

  1. 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*
    
  2. 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.

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

  1. 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*
    
  2. 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 | cut -f2 -d"="

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

Provided steps to monitor lid close and removed previous solution for watching sleep action
Source Link
M K
  • 11.1k
  • 1
  • 36
  • 55

From this answerThere does not seem to Possible to run scripts on sleep and wake?, the bestbe a way seemsfor a program to get notified when the lid is closed. However, the state of the lid can be to use a third party app - Scenario (paid) or SleepWatchermonitored periodically (freeand in this particular case, every second) and actions taken based on the lid state.

Caveats about sleep actions fromI do not know of any program that can provide a complete solution for your need, but you can use the Scenario programming guide:following information to build a solution yourself.

###Sleep Scripts Sleep Scripts are launched right before the computer goes to sleep. Again, there are some important caveats with this type of script.

  1. Download this clamshellstate.pl perl script

  2. You can run it in Terminal (under /Applications/Utilities) like:

    ./clamshellstate.pl 1  
    

When your computer goes to sleep, each program gets asked if it is ok to go to sleep and if not, the program gets a few seconds before they are forced to sleep. This way, no program can stop the computer from going to sleep and thus drain the battery and possibly create data loss.

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

The way Scenario deals with Sleep scripts is that it does not reply when the system asks it to sleep until all the scripts return that they are ready. This means your scripts must return a special error code the same way Log-out scripts do.

  1. 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*
    
  2. Configure this script to run once every second using launchd.plist. Or use Lingon to easily configure launchd.plist.

If Scenario does not reply to the System that it is ready, the System will put the computer to sleep anyway. This happens after a few seconds (about 15). So whether your script replies that it is done or not, the computer will go to sleep anyway.

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

The difference between Sleep scripts and Log-out scripts is that Log-out scripts can cancel Log-out altogether, but Sleep scripts can only delay sleep from happening. This means that if your script takes too much time to run, it will be interrupted and the computer will go to sleep.

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

This also means that if your script does not reply when it is done, your computer will wait until the default time out happens (15 seconds) which will add an unnecessary delay before Sleep occurs.

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

From this answer to Possible to run scripts on sleep and wake?, the best way seems to be to use a third party app - Scenario (paid) or SleepWatcher (free).

Caveats about sleep actions from the Scenario programming guide:

###Sleep Scripts Sleep Scripts are launched right before the computer goes to sleep. Again, there are some important caveats with this type of script.

When your computer goes to sleep, each program gets asked if it is ok to go to sleep and if not, the program gets a few seconds before they are forced to sleep. This way, no program can stop the computer from going to sleep and thus drain the battery and possibly create data loss.

The way Scenario deals with Sleep scripts is that it does not reply when the system asks it to sleep until all the scripts return that they are ready. This means your scripts must return a special error code the same way Log-out scripts do.

If Scenario does not reply to the System that it is ready, the System will put the computer to sleep anyway. This happens after a few seconds (about 15). So whether your script replies that it is done or not, the computer will go to sleep anyway.

The difference between Sleep scripts and Log-out scripts is that Log-out scripts can cancel Log-out altogether, but Sleep scripts can only delay sleep from happening. This means that if your script takes too much time to run, it will be interrupted and the computer will go to sleep.

This also means that if your script does not reply when it is done, your computer will wait until the default time out happens (15 seconds) which will add an unnecessary delay before Sleep occurs.

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

  1. 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*
    
  2. 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.

Source Link
M K
  • 11.1k
  • 1
  • 36
  • 55

From this answer to Possible to run scripts on sleep and wake?, the best way seems to be to use a third party app - Scenario (paid) or SleepWatcher (free).

Caveats about sleep actions from the Scenario programming guide:

###Sleep Scripts Sleep Scripts are launched right before the computer goes to sleep. Again, there are some important caveats with this type of script.

When your computer goes to sleep, each program gets asked if it is ok to go to sleep and if not, the program gets a few seconds before they are forced to sleep. This way, no program can stop the computer from going to sleep and thus drain the battery and possibly create data loss.

The way Scenario deals with Sleep scripts is that it does not reply when the system asks it to sleep until all the scripts return that they are ready. This means your scripts must return a special error code the same way Log-out scripts do.

If Scenario does not reply to the System that it is ready, the System will put the computer to sleep anyway. This happens after a few seconds (about 15). So whether your script replies that it is done or not, the computer will go to sleep anyway.

The difference between Sleep scripts and Log-out scripts is that Log-out scripts can cancel Log-out altogether, but Sleep scripts can only delay sleep from happening. This means that if your script takes too much time to run, it will be interrupted and the computer will go to sleep.

This also means that if your script does not reply when it is done, your computer will wait until the default time out happens (15 seconds) which will add an unnecessary delay before Sleep occurs.