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. The following script does what I want it to do but there are a few issues. Firstly, the script runs slowly and users are able to stop the script mid way by just clicking on the system preferences window when it pops up. And secondly I can’t seem to get the script to run using Workgroup Manager, possibly because I’m not able to save the file in a .sh format using AppleScript.
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_button_item(5)
keystroke return
end tell
end tell
quit
end tell
on set_button_item(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_button_item
Could anyone help me out? Some advice on how to make the script fast enough to nor give users enough time to click anything or if possible, to run the script in the background.
Maybe there is a better way to accomplish what I need. I know the preference file for this option is located in .GlobalPreferences.HARDWARE-UUID.plist but the problem with modifying this plist file is this that it is unique to a particular Mac because of the hardware UUID, not ideal when I have to push this out to about 30 systems. The file is also located in the users home aera which would make it difficult to push via Workgroup Manager.
So that’s about it. Sorry for the long post but I really need this resolved. I just need to get rid of these options for users. Is there a way of speeding up the above script so users can’t stop it? Can it be run in the background? How do I go about saving the script and get it working with Workgroup Managers login scripts option? Is there another way of resolving this? Maybe even a script to first identify the Mac’s UUID and then write to the .GlobalPreferences.HARDWARE-UUID.plist file in the users home area? Or something completly different that I haven’t even thought about it?
Oh and this is all running on Snow Leopard 10.6!
Thanks in advance and I hope someone has an answer to this.
Here’s an alternative version of your GUI script. Possibly a tad faster and it doesn’t ‘activate’ System Preferences ” which means SP opens directly in the required pane (giving the user less warning) and doesn’t come to the front (making it harder for the user to click it).
Edit: Modified in the light of quaium’s feedback below so that it now puts a Finder window over the System Preferences panel while the changes are being made. Should annoy the hell out of users.
-- Putting the code for the applications inside a script object run with 'run script' makes the execution a little faster if the script itself is being run as an application.
script
-- Get the position of System Preferences's main window, visible or not.
tell application "System Preferences"
set {x, y} to bounds of (window 1 where not (name is missing value or name is "Panel" or name is "Window"))
end tell
-- Make a Finder window (to the startup disk, say) and set it to cover the area where System Preferences's "Keyboard" window is/will be.
tell application "Finder"
activate
set Fw to (make new Finder window to startup disk)
set FwBounds to Fw's bounds
set Fw's bounds to {x, y, x + 668, y + 597}
end tell
-- "Reveal" System Preferences's modifier keys sheet behind the Finder window.
tell application "System Preferences"
reveal anchor "keyboardTab_ModifierKeys" of pane id "com.apple.preference.keyboard"
end tell
-- GUI Script the "Control (^) Key" popup menu in the sheet to show "No action".
tell application "System Events"
tell application process "System Preferences"
tell sheet 1 of window 1 -- "Modifier Keys." sheet of "Keyboard" panel.
repeat until (it exists) -- Make sure the sheet's opened before continuing.
delay 0.2
end repeat
set controlPopup to first pop up button whose name contains "Control" -- "(⌃)" doesn't work on quaium's machine, for some reason.
tell controlPopup
click
tell menu 1
repeat until (it exists) -- Wait while the menu opens.
delay 0.2
end repeat
click menu item -1 -- "No action"
repeat while (it exists) -- Wait while the menu closes.
delay 0.2
end repeat
end tell
end tell
click button 3 -- The sheet's "OK" button.
repeat while (it exists) -- Wait while the sheet closes.
delay 0.2
end repeat
end tell
end tell
end tell
tell application "System Preferences" to quit
-- Restore the Finder window's original dimensions and close it.
tell application "Finder"
set Fw's bounds to FwBounds
close Fw
end tell
end script
run script result
I haven’t been able to identify the setting involved, but to find the file itself, you could do something like this:
-- Identifify the ".GlobalPreferences.<HARDWARE-UID>.plist" file.
do shell script "find -f ~/Library/Preferences \\( -name '.GlobalPreferences.*.plist' \\)"
-- Identify and read it:
do shell script "find -f ~/Library/Preferences \\( -name '.GlobalPreferences.*.plist' \\) | cut -d '.' -f 1-3 | defaults read"
Hope this helps. I’m afraid I don’t know anything about Workgroup Manager.
Thank you Nigel. That worked, but I had to change the line “set controlPopup to first pop up button whose name contains “(⌃)”” to “set controlPopup to first pop up button whose name contains “Control”” because it kept on throwing up an error message about not recognising “(⌃)”.
It does move a lot faster but I was wondering do you think you could edit the script to maybe put up another window in front of the system preference panel? I just want to be 100% sure users won’t be able to stop the process (I managed to stop it and we are dealing with children here and boy will they try and try!).
Maybe I could open up a picture in front saved on the local hard drive and then it closes along with the script. Not sure if this would be possible, as I’m wondering how you’d get a second screen up in the same position.
You probably weren’t able to identify the file .GlobalPreferences.HARDWARE-UUID.plist file because it is hidden.
Not very nice, but I’ve now edited it above. I tried moving the System Preferences panel off-screen altogether, which would have been better; but it won’t go.
Yes, thanks. I knew about the file, but couldn’t find the setting inside it for the modifier keys. I imagine too that some process has to be “killalled” to make any new settings take, but I don’t know which.
Thank you very very very much! It works perfectly. I’ve been working on this for ages now and finally I have something I can actually use and roll out.