How can I make the power button on my MacBook run a custom script? I'm running OS 10.7.5 on an October 2008 unibody MacBook, and I would like to somehow get my computer to run a script whenever I press the power button, instead of prompting me if I want to turn my laptop off.

My situation is as follows: simply closing the cover of the MacBook sometimes results in Sleep mode not initiating, and the computer then closed gets very hot (not sure what's going on in there). I solved this problem by pressing the Power button and choosing "Sleep" each time I want to put it to sleep, and then closing the lid.

Now, as I use TrueCrypt, I want to unmount all my TrueCrypt drives whenever I put my laptop to sleep. So when I press the power button, I wish to run the following commands:

truecrypt -d
osascript -e 'tell application "System Events" to sleep'


In this case it doesn't have to give me any type of prompt, I would like the commands to be executed directly. Any suggestions appreciated!

EDIT: I guess it doesn't have to be the power button that I press when this script is executed. That is, if I can get the execution of these two commands associated with a certain combination of keys, I would be just as happy.
KeyRemap4MacBook can apparently remap the power button for some Macs, but it didn't work with my iMac.

&lt;autogen&gt;__ConsumerToKey__ ConsumerKeyCode::POWER, KeyCode::F19&lt;/autogen&gt;


If you just want to assign a shortcut to a script, see this question.