Saturday, April 16, 2011

PC keyboard for Mac: Applescript for checkboxes in DoubleCommand preference panel

A keyboard shared with a PC at home, a keyboard for OS X at work: what's a laptop to do? Switch automatically, please.

The handy DoubleCommand extension to OS X system preferences will swap Command and Option (Alt and Meta) keys, but that entails a manual operation twice per day.

Several free and paid apps will help automate changes when the OS X "Location" changes. Marco Polo works fine for my purposes and is free and GPL. Any of the Location-changing tools will do, since they will all open Applescript files.

That leaves the actual changing of the preferences. Apple doesn't make much Applescript-recordable software nowadays, so recording doesn't work. Instead, one must make OS X available to scripting via the Universal Access panel and then figure out the names of the GUI elements of interest.

To figure out the names of the GUI elements of interest in a OS X preference pane, use the free Apple tool UIElementInspector as described well at MakeUseOf.

Ok, if you read this far, you know how to do what I did. Here, then, are the Applescripts for turning a PC keyboard into a Mac keyboard, at least for swapping the Command & Option (Alt & Meta) keys.


The command for the PC keyboard location, name it "swapKeys.scpt" or something similar

tell application "System Events"
 if UI elements enabled is false then
  tell application "System Preferences"
   activate
   set current pane to pane id "com.apple.preference.universalaccess"
   display dialog "This script requires access for assistive evices be enabled." & return & return & "To continue, click the OK button and enter an administrative password in the forthcoming security dialog. (Then the switch will be set for you. You can always undo that change later in the System Preferences, 'Universal Access' pane.)" with icon 1
  end tell
  set UI elements enabled to true
  if UI elements enabled is false then return "user cancelled"
  delay 1
 end if
end tell

tell application "System Preferences"
 activate
 reveal (pane "DoubleCommand")
end tell

tell application "System Preferences"
 tell application "System Events"
  tell process "System Preferences"
   tell list 1 of window 1
    set swapped to value of checkbox "Command Key acts as Option Key" as boolean
    
    if swapped is false then
     click checkbox "Command Key acts as Option Key"
     click checkbox "Option Key acts as Command Key"
    end if
    
   end tell
   tell window 1
    click button "Activate"
   end tell
  end tell
 end tell
 quit
end tell

The script for the OS X keyboard. Name it 'normalKeys.scpt' or something similar


tell application "System Events"
 if UI elements enabled is false then
  tell application "System Preferences"
   activate
   set current pane to pane id "com.apple.preference.universalaccess"
   display dialog "This script requires access for assistive evices be enabled." & return & return & "To continue, click the OK button and enter an administrative password in the forthcoming security dialog. (Then the switch will be set for you. You can always undo that change later in the System Preferences, 'Universal Access' pane.)" with icon 1
  end tell
  set UI elements enabled to true
  if UI elements enabled is false then return "user cancelled"
  delay 1
 end if
end tell

tell application "System Preferences"
 activate
 reveal (pane "DoubleCommand")
end tell

tell application "System Preferences"
 tell application "System Events"
  tell process "System Preferences"
   tell list 1 of window 1
    set swapped to value of checkbox "Command Key acts as Option Key" as boolean
    
    if swapped is true then
     click checkbox "Command Key acts as Option Key"
     click checkbox "Option Key acts as Command Key"
    end if
   end tell
   tell window 1
    click button "Activate"
   end tell
  end tell
 end tell
 quit
end tell