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:
In a generic mouse scenario, the following example ApplesScript code changes the value of the Tracking Speed slider:
The value range of the Tracking Speed slider is a decimal value between
0
and9
. Note that the value range for the other sliders differ, Double-Click speed is0
to10
, and Scrolling Speed is0
to7
. 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:
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 additionaldelay
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, theslider
number is different, along with itsname
. In the first version it'sslider 1
andTracking
where as in the second it'sslider 3
andTracking Speed
. There is also notab 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:
Can be writen as:
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 beslider 3
. The same concept can apply to otherUI elements
too.You can also address the
slider
by itsname
, e.g.: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.