开发者

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开发者_如何学Python 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?


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


If you are using Mavericks 10.9 there is an extra tab group you need to consider, this script works:

set trackingValue to 8

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

            tell tab group 1 of window "Mouse"
                set value of slider "Tracking" to trackingValue
            end tell
            --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


Jason's suggestion seems not working in 10.8. I tried to replace "Tracking Speed" with "Tracking", the value was not set correspondently.

Wonder they've changed the name of the slider or not.


The script still functions as intended with OS X Lion 10.7. One small change I added after setting the Tracking Speed value is:

        set value of slider "Tracking Speed" of window "Mouse" to trackingValue
        tell application "System Preferences" to quit
        --end tell

This closes System Preferences so I'm not left looking at a menu pane and can get back to my work with my mouse's now speedier tracking.


This works on my Sierra OS X machine. If you get an error, you may have to slightly lengthen one or both of the script delays to better match the speed your machine.

set trackingValue to 8

--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
            delay 1
            --Open the "Mouse" pane
            click menu item "Mouse" of menu "View" of menu bar 1
            delay 0.5
            tell window "Mouse"
                set value of slider "Tracking speed" to trackingValue
            end tell
        on error theError
            --An error occured
            display dialog ("Sorry, an error occured while altering Mouse settings:" & return & theError) buttons "OK" default button "OK"
        end try
    end tell
end tell

tell application "System Preferences" to quit


For some reason those scripts were not working in OS X 10.10. Needed a little tweaking but now it works.

set trackingValue to 9

--Open and activate System Preferences
tell application "System Preferences" to activate
tell application "System Preferences"
    reveal pane "com.apple.preference.mouse"
end tell
--Attempt to change settings using System Events
tell application "System Events"
    tell process "System Preferences"
        tell tab group 1 of window "Mouse"
            set value of slider "Tracking" to trackingValue
        end tell
    end tell
end tell
tell application "System Preferences" to quit

Enjoy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜