Search code examples
osx-snow-leopardapplescript

Setting the mouse tracking speed via applescript


I'm trying to get an apple script to set the mouse tracking speed in OS X 10.6, specifically with a new Magic Mouse.

I've found the following:

set trackingValue to 5

--Open and activate System Preferences
tell application "System Preferences" to activate

--Attempt to change settings using System Events
tell application "System Events"
    tell process "System Preferences"
        try
            --Open the "Keyboard & Mouse" pane
            click menu item "Mouse" of menu "View" of menu bar 1
            delay 2
            set value of slider 1 to trackingValue
            --end tell
        on error theError
            --An error occured
            display dialog ("Sorry, an error occured while altering Keyboard and Mouse settings:" & return & theError) buttons "OK" default button "OK"
        end try
    end tell
end tell

But it seems to be built for 10.5 as I get an error when trying to 'set value of slider 1 to trackingValue'

2 questions...

  1. How can I get this to work with the 10.6/Magic Mouse combo?
  2. How would I go about getting the name of the slider on the control panel, or any other control for that matter, for use in applescript?

Solution

  • Two things here - firstly, you need to have checked the "Enable access for assistive devices" in System Preferences / Universal Access / Mouse & Trackpad. Obviously you've already done this, otherwise the script wouldn't get to where it is without already failing, but it's an important step for anyone else trying to get this working.

    Secondly, the problem with the line you were receiving the error on was that you weren't telling the AppleScript where to find the slider you were wanting to change the value of. By changing the line to the following, the script started working:

    set value of slider "Tracking Speed" of window "Mouse" to trackingValue
    

    Note that as well as naming the window to be used by the AppleScript I have also named the slider to be used as well. Whilst running Snow Leopard and using "slider 1", the second slider in the window "Scrolling Speed" was being altered. So, by using the name of the slider rather than its number we bypass any possible indexing issues. As for working out the name of the slider? I simply tried using the value of the label that went with it, which worked in this instance. Your mileage may vary, of course.

    Thus, the final script becomes:

    set trackingValue to 5
    
    --Open and activate System Preferences
    tell application "System Preferences" to activate
    
    --Attempt to change settings using System Events
    tell application "System Events"
        tell process "System Preferences"
            try
                --Open the "Keyboard & Mouse" pane
                click menu item "Mouse" of menu "View" of menu bar 1
                delay 2
                set value of slider "Tracking Speed" of window "Mouse" to trackingValue
                --end tell
            on error theError
                --An error occured
                display dialog ("Sorry, an error occured while altering Keyboard and Mouse settings:" & return & theError) buttons "OK" default button "OK"
            end try
        end tell
    end tell