Most of the time, I like the "caps lock" to be mapped to the control key. Sometimes, I want it to be bound to "caps lock". I want to switch it in a shell script, so I can do it automatically or via a keyboard shortcut.
The settings are stored in
~/Library/Preferences/ByHost/.GlobalPreferences*.plist, but I don't know any way to apply changes to it without logging out and back in. KeyRemap4MacBook has a command line utility that allows disabling and enabling settings, but it doesn't work with PCKeyboardHack.
This only changes the settings for one keyboard and doesn't work if the system language is not English.
tell application "System Preferences" reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard" end tell tell application "System Events" to tell window 1 of process "System Preferences" click button 1 of tab group 1 tell sheet 1 tell pop up button 4 click delay 0.1 if value is "⇪ Caps Lock" then click menu item 2 of menu 1 else click menu item 1 of menu 1 end if end tell click button "OK" end tell end tell quit application "System Preferences"