Yesterday I found a handy little command line utility for checking whether a modifier key is held down. It’s great for scripting of all kinds. The solution came from this post on the Apple Mailing List:
The code compiles into a little utility called keys. Here’s a version compiled on Mountain Lion that you can download if you don’t have the developer tools installed: keys.zip. If you do have clang or gcc and the Carbon framework available, run this to compile:
clang keys.m -framework Carbon -o keys
Move the resulting keys file into /usr/local/bin/ and make sure that’s in your path. Now, from a shell script (or with AppleScript’s do shell script command), you can run something like keys option to see if the Option key is held down at the time. If you put a sleep 2 before it in a shell script, you can press a modifier key at runtime and change the behavior accordingly.
As an example, here’s the AppleScript for launching your Dock apps from the other day, with the ability to exclude certain apps if the Option key is held down: