Change keyboard modifier keys automatically on OSX with Applescript
Here is an elegant solution to a common problem which has troubled a large number of otherwise happy Macbook, and Macbook Pro users over the past few years. The scenario is this: you use your shiny apple laptop both at the office and at home. At the office, you use whatever accessories the friendly IT staff has provided you. This generally includes a generic windows based keyboard, cute little windows key and all. For those not familiar with the problem this creates, don’t bother reading the rest of this - I promise it will be boring. If this is something you have encountered on the other hand, I don’t need to tell you that the switching of Apple key and Option key (alt key and windows key on a windows keyboard) is enough to drive a mac user crazy.
Technorati Tags: Applescript, keyboard, OSX
Fortunately, Apple has given us a relatively easy way of switching the functionality of these two keys, allowing a windows keyboard to act just like your built in keyboard, or an apple external keyboard. Under System Preferences -> Keyboard and Mouse -> Keyboard, you will see a button on the lower left called “Modifier Keys…” Clicking that will present you with an opportunity to manually switch these keys around. If you always use the same keyboard, or never plan to use both the external windows keyboard and the internal keyboard on your laptop, you are done. Those of us that actually use the laptop as, you know, a mobile computer are still left with frustration anytime we switch environments and keyboards. Put simply, it sucks to have to go through this process every time you move your machine.
Applescript to the rescue! Lance Ball has written a very handy Applescript which will quickly and cleanly open your system preferences and switch the behavior of these two keys. The script (see below for the code) can easily be run manually whenever there is a need to flip-flop these keys. I wanted to take this just one step further and have complete automation of this process. Basically I want my computer to know where it is, and change it’s behavior accordingly without me having to think about it. Fortunately, there is a wonderful open source package available that provides exactly this functionality (and a lot more.) By using MarcoPolo on my macbook pro, I can setup a number of rules which the software can use to actively determine the machines current location. Once the location has been successfully determined (generally only takes a few seconds) a number of actions can be executed including changing networking settings (ie. enabling a proxy) and pursuant to the topic of this post, running an applescript.
I’ve set this up and it works wonderfully. Upon opening the computer in my office, MarcoPolo detects it’s location and automatically sets up my keyboard preferences accordingly. When I unplug for the day and head home, it again automatically understands that I’m not longer connected to that keyboard and reconfigures my system back to the standard Apple configuration - very nice!
The following is the unmodified script written by Lance Ball. I’m reposting it here as his website is currently under re-construction and the code is unavailable for download there. I have personally modified this script a bit to work more elegantly with the MarcoPolo setup described above, but wanted full credit for the code to go to Lance.
UPDATE Feb 2008: The following code applies to OSX 10.4 and earlier only. Updated applescript for 10.5 can be found below (here) thanks to commenter Joe Thomas.
-- **********************************************************************************
-- Utility script to switch keyboard mapping for Command and Option keys.
-- Useful when you have a PC external keyboard that you use in one location, but
-- at other times you are using the builtin laptop keyboard or an Apple keyboard.
-- Author: Lance Ball (lanceball - at - mac - dot - com)
-- **********************************************************************************
-- Open System Preferences
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
-- If we don't have UI Elements enabled, then nothing is really going to work.
if UI elements enabled then
tell application process "System Preferences"
get properties
-- Open up the Modifier Keys sheet
click button "Modifier Keys…" of tab group 1 of window "Keyboard & Mouse"
tell sheet 1 of window "Keyboard & Mouse"
-- get the text of the 3rd pop up button
set commandKey to value of pop up button 3
-- looks like we're in default mode. Swap the keys
if commandKey ends with "Option" then
click pop up button 3
click menu item 4 of menu 1 of pop up button 3
delay 1
click pop up button 4
click menu item 3 of menu 1 of pop up button 4
else
-- We're in PC keyboard mode. Swap back to the defaults
click button "Restore Defaults"
end if
-- close the sheet
click button "OK"
end tell
end tell
tell application "System Preferences" to quit
else
-- UI elements not enabled. Display an alert
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
display dialog "UI element scripting is not enabled.
Check \"Enable access for assistive devices\""
end tell
end if
end tell
January 30th, 2009 at 8:44 am
[...] For even more convenience, check out Red Sweater Software’s FastScripts. It lets you assign keyboard shortcuts to scripts in your AppleScript menu. This script is based on a post at fall-line.com. [...]