How to use AppleScript GUI scripting to change the mouse speed

applescriptscript

I use a Mac at school which resets all settings once logged out. I would like to automatically set some settings in system preferences.

I found an answer here which described how to change the natural scrolling behavior in system preferences. Unfortunately, this doesn't help in changing the mouse speed. Additionally, I've noticed that fining the checkbox number in AppleScript is a very hit-or-miss process. Checkbox 1 might activate a checkbox at the bottom of a particular settings page, whereas Checkbox 2 might activate one in the middle somewhere. I feel uncertain whether I'd be able to find out how to change a slider due to this 'randomness.'

Does anyone know how to change the slider using GUI scripting?

Best Answer

As you've no doubt found out, UI Scripting has its issues and in this particular case, the mouse preferences, the code can vary based on the version of the OS and what type of mouse, thus making it difficult to provide you code that will absolutely work in your use case scenario. To show you, I'll use two examples of code that work for me on my system.

On a MacBook Pro with an Apple Magic Mouse under OS X 10.8.6 the following example ApplesScript code changes the value of the Tracking slider:

if application "System Preferences" is running then ¬
    tell application "System Preferences" to quit
delay 1
tell application "System Preferences"
    reveal anchor "mouseTab" of pane id "com.apple.preference.mouse"
    delay 1
    tell application "System Events"
        click of radio button 1 of tab group 1 of window 1 of application process "System Preferences"
        set value of slider 1 of tab group 1 of window 1 of application process "System Preferences" to 4
    end tell
    quit
end tell

Apple Magic Mouse preferences


In a generic mouse scenario, the following example ApplesScript code changes the value of the Tracking Speed slider:

if application "System Preferences" is running then ¬
    tell application "System Preferences" to quit
delay 1
tell application "System Preferences"
    reveal anchor "mouseTab" of pane id "com.apple.preference.mouse"
    delay 1
    tell application "System Events"
        set value of slider 3 of window 1 of application process "System Preferences" to 4
    end tell
    quit
end tell

generic mouse preferences


The value range of the Tracking Speed slider is a decimal value between 0 and 9. Note that the value range for the other sliders differ, Double-Click speed is 0 to 10, and Scrolling Speed is 0 to 7. Also note that these ranges may very depending on the OS version and hardware.

With System Preferences opened to the Mouse pane, for a generic mouse scenario, use the following line of code in Script Editor to get the properties of the sliders:

tell application "System Events" to get properties of every slider of window 1 of application process "System Preferences"

You'll see the minimum/maximum values as well as the value they are currently set to. The later is important to know after you've manually adjusted to your liking and then use in the script to make the changes as wanted.

Note that with UI Scripting the value of the delay commands may need to be adjusted for your environment and or additional delay commands may be necessary as appropriate.

Also note that the example ApplesScript code is coded to first close System Preferences if it's open. This is being done so as not to have to see the events taking place as the graphic dance of some UI Scripting can be distracting.


You will see the difference between the two versions of example ApplesScript code in the second one, not only does it not have or need the click of radio button ... command, the slider number is different, along with its name. In the first version it's slider 1 and Tracking where as in the second it's slider 3 and Tracking Speed. There is also no tab group in the second version.

To address the hit to miss point mentioned in the OP, with the generic mouse scenario there are three sliders, so the following line of code for the tracking speed:

set value of slider 3 of window 1 of application process "System Preferences" to 4

Can be writen as:

set value of every slider of window 1 of application process "System Preferences" whose name contains "track" to 4

Now you don't need to know what number the slider is as it will act on the only one whose name contains "track", which in this case will be slider 3. The same concept can apply to other UI elements too.

You can also address the slider by its name, e.g.:

set value of slider "Tracking Speed" of window 1 of application process "System Preferences" whose name contains "track" to 4

The same goes for other UI elements that have a name property, in this case e.g., window 1 is also: window "Mouse"


Note: The example ApplesScript code is just that and does not contain any error handling as may be appropriate/needed/wanted. The onus is upon the user to add error handling as may be appropriate/needed/wanted.