Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script to change keyboard modifer keys?

I'm trying to create a login script to use with Workgroup Manager that will allow me to change the keyboard modifer keys on a client machine. I've done a search and found a few scripts but they don't seem to work. For example the following script:

-------------------------------------------------------------------------------
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
-------------------------------------------------------------------------------

I am trying to get this to work on OS X 10.6 with no luck. Firstly I have seperate options in System Preferences for Keyboard and Mouse settings (the script above points to window "Keyboard & Mouse") and secondly, even after changing the "Keyboard & Mouse" to just "Keyboard" I get an error message about not being about to locate button "Modifier Keys...".

All I'm trying to do is change the Modifier Keys so that Crtl key is set to nothing. Could anyone help me out?

Mac Mini, Mac OS X (10.6.5)

Posted on Mar 24, 2011 4:00 AM

Reply
Question marked as Best reply

Posted on Mar 24, 2011 3:16 PM

This could work... (tested with 10.6.7)... not very fast...
another idea could be a defaults write command, but I am not sure which prefs-file you would have to edit.. maybe .GlobalPreferences.plist...

tell application "System Preferences"

activate

set current pane to pane "com.apple.preference.keyboard"

tell application "System Events"

tell process "System Preferences"

click button 1 of tab group 1 of window 1

set curOpt to (get value of pop up button 3 of sheet 1 of window 1)


set curCmd to (get value of pop up button 4 of sheet 1 of window 1)


click pop up button 4 of sheet 1 of window 1

my setbuttonitem(1)


click pop up button 3 of sheet 1 of window 1

my setbuttonitem(2)


click pop up button 2 of sheet 1 of window 1

my setbuttonitem(3)


click pop up button 1 of sheet 1 of window 1

my setbuttonitem(4)

delay 0.25

keystroke return

end tell

end tell

quit

end tell

on setbuttonitem(valueindex)

tell application "System Events"

tell process "System Preferences"

repeat with i from 1 to 5

key code 126

delay 0.25

end repeat

repeat with i from 1 to valueindex - 1

key code 125

delay 0.25

end repeat

keystroke return

end tell

end tell


end setbuttonitem

16 replies
Question marked as Best reply

Mar 24, 2011 3:16 PM in response to quaium

This could work... (tested with 10.6.7)... not very fast...
another idea could be a defaults write command, but I am not sure which prefs-file you would have to edit.. maybe .GlobalPreferences.plist...

tell application "System Preferences"

activate

set current pane to pane "com.apple.preference.keyboard"

tell application "System Events"

tell process "System Preferences"

click button 1 of tab group 1 of window 1

set curOpt to (get value of pop up button 3 of sheet 1 of window 1)


set curCmd to (get value of pop up button 4 of sheet 1 of window 1)


click pop up button 4 of sheet 1 of window 1

my setbuttonitem(1)


click pop up button 3 of sheet 1 of window 1

my setbuttonitem(2)


click pop up button 2 of sheet 1 of window 1

my setbuttonitem(3)


click pop up button 1 of sheet 1 of window 1

my setbuttonitem(4)

delay 0.25

keystroke return

end tell

end tell

quit

end tell

on setbuttonitem(valueindex)

tell application "System Events"

tell process "System Preferences"

repeat with i from 1 to 5

key code 126

delay 0.25

end repeat

repeat with i from 1 to valueindex - 1

key code 125

delay 0.25

end repeat

keystroke return

end tell

end tell


end setbuttonitem

Mar 24, 2011 4:17 PM in response to hubionmac

Thank you for that. I'll give it a go tomorrow. Does the script you provided only change the ctrl key to nothing and leaves the rest at default? Only reason why I ask is because you have curOpt and curCmd in the script?

Talking about the preference file I have been trying to find out which one it is for the past few days now and can't seem to figure it out.

Mar 24, 2011 6:05 PM in response to quaium

Hi!

Talking about the preference file I have been trying to find out which one it is for the past few days now and can't seem to figure it out.


~/Library/Preferences/ByHost/.GlobalPreferences.YOURHARDWARE-UUID.plist

Since a modified Plist will send no notification changes will take effect only after log out/in

Mar 25, 2011 4:57 AM in response to hubionmac

Thank you very much hubionmac. Your script above does the job.

One other thing, when saving the script, what format do I save it in to allow me to run it as a Workgroup Manager login script. From research I understand that it has to be in a .sh format but AppleScript doesn't give me that option.

Or is there a way of running this as a startup application as I can save the file in a .app format?

Mar 25, 2011 9:33 AM in response to quaium

ARGH!! Users can stop this from running when it is executed. All they need to do is click on the dialogue box and everything stops. ARGH!! Is there no way of running this in the background?

Also answers to the above questions would be nice (sorry, I posted that from another account). This is driving me crazy User uploaded file

Mar 26, 2011 4:19 PM in response to spazek

A quick background to this is to stop users using the ctrl- click method to use as right click. The reason why I want to stop this is so that they can't right in on Finder in the dock and access all the options as well as right clicking on the Finder window itself to edit sidebar preferences.

The only problem with modifying a plist file is the case is the fact that the modifier keys change is saved in the .gobalpreferences file that has the unique ID of the particular. So even if I could do a defaults write script I would need to be able to change the file on different macs. If there is a script to do that I would love to know about it.

Mar 28, 2011 3:43 AM in response to quaium

Hi!

I had a closer look at your problem today. It is a little more difficult than i thought.
Run a script on YOUR Mac is easy but to run it on other and maybe unknown Macs there are a lot of things to look for.

• There a two GlobalPreferences Plists in the ByHost Folder. The first one is named with a hardware UUID. The second one has a MAC like looking identifier. These two Plists are handled in a different way on Desktop and Notebook Macs, that means the needed Plist entries will be done in either the first OR the second one.
• There is another unique number needed for one of the Plist entries.

If you would knew all these identifiers for every single Mac it would be possible to write a script.
But this is a lot of work.

It will be a lot easier use a GUI-Script in the way it was posted by hubionmac. Making some changes you can speed it up, so there will be less time for a user to cancel it.

Spazek

Mar 28, 2011 4:07 AM in response to spazek

spazek Thank you for looking in to this. I've tried a few things and have found that the .GlobalPreferences.HARDWARE-UUID.plist is the file that needs to be altered. However, and the thing that makes it difficult is what you pointed out about the plist file being machine specific.

So I guess the only way would be to use the GUI-Script. Two questions, firstly how would I go about speeding up the process. I have modified hubionmac's script to read as follows:

--------------------------------------------------------------------------------
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
tell application "System Events"
tell process "System Preferences"
click button 1 of tab group 1 of window 1
set curOpt to (get value of pop up button 3 of sheet 1 of window 1)
set curCmd to (get value of pop up button 4 of sheet 1 of window 1)
click pop up button 3 of sheet 1 of window 1
my set buttonitem(5)
keystroke return
end tell
end tell
quit
end tell
on set buttonitem(valueindex)
tell application "System Events"
tell process "System Preferences"
repeat with i from 1 to 5
key code 126
end repeat
repeat with i from 1 to valueindex - 1
key code 125
end repeat
keystroke return
end tell
end tell
end set buttonitem
-------------------------------------------------------------------------------- -

What would I need to add to the above script to speed things up? Secondly, how do I execute this script on our Macs? Do I need to save the file in a .sh format? If so, how do I do that as AppleScript doesn't allow me to save a script in that format? And lastly, is it better to push the script out via Workgroup Manager or another method?

Sorry for all these questions, I'm fairly new to the Mac environment and need this quite urgently. Thanks again.

Mar 28, 2011 4:13 AM in response to spazek

I think since 10.5.5 apple switched from using MAC Address to UUID on theses plist file names. So on machines using systems older than 10.5.5

macs_MAC()

on macs_MAC()

set myMACaddress to (do shell script "ifconfig en0 ether | grep -i ether" as string) as string

set AppleScript's text item delimiters to " "

set b to every text item of myMACaddress

set AppleScript's text item delimiters to ""

repeat with k in b

if k contains ":" then

set AppleScript's text item delimiters to ":"

set myMAC to every text item of k

set AppleScript's text item delimiters to ""

return myMAC as text

exit repeat

end if

end repeat

end macs_MAC



give me some time to think about it...

Script to change keyboard modifer keys?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.