I was able to modify some of the scripts already out there, and get them to work in Lion. To create these scripts:
- Run the Applescript Editor
- Create two new empty script files (command-N)
- Paste in these two scripts
- Save them as something like "swap command option" and "restore command option" or whatever you'd like
- You can test them by running them in the applescript editor.
Here is the script to swap command to option, and option to command:
#
# Script to swap the Command and Option keys
# in the System Preferences Keyboard settings.
#
# Helpful if using a PC keyboard
#
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Modifier Keys…" of tab group 1 of window "Keyboard"
# Select keyboard: pop up button
click pop up button 5 of sheet 1 of window "Keyboard"
# The 4th choice there.. my USB Keyboard
click menu item 4 of menu 1 of pop up button 5 of sheet 1 of window "Keyboard"
# The Option Key pop up
click pop up button 2 of sheet 1 of window "Keyboard"
# Change it to Command, the 4th choice
click menu item 4 of menu 1 of pop up button 2 of sheet 1 of window "Keyboard"
# The Command Key pop up
click pop up button 1 of sheet 1 of window "Keyboard"
# Change it to Option, the 3rd choice
click menu item 3 of menu 1 of pop up button 1 of sheet 1 of window "Keyboard"
click button "OK" of sheet 1 of window "Keyboard"
end tell
end tell
tell application "System Preferences"
quit
end tell
Here is the script to swap them back:
#
# Script to restore the Command and Option keys to their defaults
# in the System Preferences Keyboard settings.
#
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Modifier Keys…" of tab group 1 of window "Keyboard"
# Select keyboard: pop up button
click pop up button 5 of sheet 1 of window "Keyboard"
# The 4th choice there.. my USB Keyboard
click menu item 4 of menu 1 of pop up button 5 of sheet 1 of window "Keyboard"
# The Option Key pop up
click pop up button 2 of sheet 1 of window "Keyboard"
# Change it to Option, the 3rd choice
click menu item 3 of menu 1 of pop up button 2 of sheet 1 of window "Keyboard"
# The Command Key pop up
click pop up button 1 of sheet 1 of window "Keyboard"
# Change it to Command, the 4th choice
click menu item 4 of menu 1 of pop up button 1 of sheet 1 of window "Keyboard"
click button "OK" of sheet 1 of window "Keyboard"
end tell
end tell
tell application "System Preferences"
quit
end tell
To make these scripts easy to access, you can go to Applescript Editor Preferences and check "Show Script menu in menu bar". Then, copy your scripts to your home directory Library/Scripts directory, i.e. /Users/ryan/Library/Scripts
Now you can access them right off the menu bar script menu.
click button "Restore Defaults" of sheet 1 of window "Keyboard"
a more elegant / generic way to go back to defaults, assuming you don't tend to mess with any of the other mappings. – Sonafets Feb 01 '20 at 17:10window "Keyboard"
reference will fail withCan’t get window "Keyboard" of process "System Preferences".
You can quick fix this by addingdelay 1
before the System Events tell. In theory it's possible to script out a wait and check for the window to be loaded... – NReilingh Jan 02 '21 at 22:58