Gui Scripting, switching modifier keys

I would like to write a simple script to swap cmd and option back and for use when I plug my powerbook into an external windows keyboard. Here is what I have so far (it breaks trying to select from the pop up button). I am not sure, but it may be due to the fact the the string representing the menu item has the icon for the modifier key embeded in it (verified with a display dialog). Just a guess though since I am new to this.

tell application “System Preferences”
activate
set current pane to pane “com.apple.preference.keyboard”
tell application “System Events”
tell process “System Preferences”
click button “Modifier Keys.” of tab group 1 of window “Keyboard & Mouse”
set curOpt to (get value of pop up button 3 of sheet 1 of window “Keyboard & Mouse”)

		set curCmd to (get value of pop up button 4 of sheet 1 of window "Keyboard & Mouse")
		
		tell sheet 1 of window "Keyboard & Mouse"
			tell pop up button 3
				click
				click menu item curCmd
			end tell
			tell pop up button 4
				click
				click menu item curOpt
			end tell
		end tell
		click button "Ok" of sheet 1 of window "Keyboard & Mouse"
	end tell
end tell
quit

end tell

For someone new to this, you’re doing remarkably well, mrodkey. :slight_smile:

Actually, all you’re missing is an additional level in the UI hierarchy - since the pop up buttons each have a menu that contains the menu items. So, to fix the immediate problem, all you need to do is change:

to:

However, once you’ve done that, I have a feeling that you may (sometimes) run into timing issues - if the clicks aren’t registered properly before any subsequent statement is executed. The following suggestion includes a delaying mechanism (and also attempts to condense the script slightly):

Edit: script updated 11 January 2006 to fix ‘NSReceiverEvaluationScriptError: 4’ issue (discussed below).

tell application "System Events" to set quitOnDone to not (exists process "System Preferences")
tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events" to tell window "Keyboard & Mouse" of process "System Preferences"
	tell tab group 1
		tell radio button "Keyboard" to if value is 0 then
			click
			repeat while value is 0
				delay 0.2
			end repeat
		end if
		click button "Modifier Keys."
	end tell
	tell sheet 1
		set valList to {value of pop up button 4, value of pop up button 3}
		repeat with n from 3 to 4
			set currVal to valList's item (n - 2)
			tell pop up button n
				click
				click menu item currVal of menu 1
				repeat until value is currVal
					delay 0.2
				end repeat
			end tell
		end repeat
		click button "OK"
	end tell
end tell
if quitOnDone then tell application "System Preferences" to quit

Thank you very much for the help … your suggestion works perfectly. The delay suggestion sounds like a good suggestion for GUI Scripting, so I will add it to my bag of tricks :wink:

I had the same problem and a little Googling gave me this fix. Go to “Universal Access” in the System Preferences, and make sure the “Enable access for assistive devices” option is checked. Apparently AppleScript goes through the accessibility APIs to do. Hope that helps!

Sorry guys - almost missed this one.

In fact, there was a script/interface issue involved here, which should now be addressed. I’ve modified the script above - and hope that this fixes it for you. :slight_smile: