Apple Script to set Primary Button, Secondary Button and other properties for Mouse Preference

戏子无情 提交于 2019-12-08 19:40:35

Here's my version which eliminates the need for the "keyboard tricks" as shown in the post from Christopher Kemp above. Plus it has a few extra things thrown in like running a check to make sure assistive devices is set to 'on', and a dialog box so you can select whether you want the mouse to be set as left or right handed. Hope it helps :)

P.S. This is for 10.6 (Snow Leopard)...you'd have to change it slightly to suit the 10.5 (Leopard) preference pane differences.

--

# check 'Enable access for assistive devices' is ON

tell application "System Events"

if UI elements enabled then

    # open dialog
    set question to display dialog "Select your mouse preference." buttons {"Left Handed", "Right Handed", "Cancel"} default button 3 with title "Mouse Switch"

    set answer to button returned of question

    if answer is equal to "Left Handed" then

        # open Mouse preferences & set left button to Secondary
        tell application "System Preferences"

            activate
            reveal pane "Mouse"

        end tell

        tell application "System Events"

            tell process "System Preferences"

                # set Secondary Button
                click pop up button 4 of group 1 of window "Mouse"
                click menu item "Secondary Button" of menu of pop up button 4 of group 1 of window "Mouse"
                # set Primary Button
                click pop up button 5 of group 1 of window "Mouse"
                click menu item "Primary Button" of menu of pop up button 5 of group 1 of window "Mouse"

            end tell

        end tell

        tell application "System Preferences" to quit

        display dialog "Your mouse is now set to: Left Handed" buttons {"OK"}

    end if

    if answer is equal to "Right Handed" then

        # open Mouse preferences & set right button to Secondary
        tell application "System Preferences"

            activate
            reveal pane "Mouse"

        end tell

        tell application "System Events"

            tell process "System Preferences"

                # set Secondary Button
                click pop up button 5 of group 1 of window "Mouse"
                click menu item "Secondary Button" of menu of pop up button 5 of group 1 of window "Mouse"
                # set Primary Button
                click pop up button 4 of group 1 of window "Mouse"
                click menu item "Primary Button" of menu of pop up button 4 of group 1 of window "Mouse"

            end tell

        end tell

        tell application "System Preferences" to quit

        display dialog "Your mouse is now set to: Right Handed" buttons {"OK"}

    end if

    # if 'Enable access for assistive devices' is OFF show error
else

    display dialog "Please select 'Enable access for assistive devices' from the Universal Access preference pane to run this script." buttons {"OK"} default button 1 with icon caution

end if

end tell

You need to do exactly what that error message says. Look under Universal Access in System Preferences, and you will see a checkbox for "Enable access for assistive devices". I'll leave it as an exercise to you to sort out how to enable that via Applescript.


Please post all of the relevant code. There are lot of reason why that might not work. The Universal Access pane may not be active, the button or the type may not be accessible via Applescript (the latter being more than likely).

goto system preferences, universal access pane, select "Enable access for assistive devices"

this needs to be enable on an mac that is going to run as script the uses GUI scripting

EDIT

now that you have done that you can change your script since it got an error, have your script click the radio button like so

  tell application "System Preferences"
   activate
   set current pane to first pane whose name is "Mouse"
  end tell

  tell application "System Events"
   tell process "System Preferences"
    try
     click radio button "Left" of every radio group of window "Mouse"
    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

ADDITIONAL INFO

check out Scriptable System Preferences you may find that thread more helpful

I got this to work:

# open Mouse preferences & set right button to Secondary

tell application "System Preferences"
    activate
    set current pane to first pane whose name is "Mouse"
end tell
tell application "System Events"
    tell process "System Preferences"

            # activates drop-down menu
        click pop up button 5 of group 1 of window "Mouse"

            # page up key, to ensure we're starting at the top of the list
        key code 116

            # down arrow, to select Secondary Button from list
        key code 125

            # Return key to make selection
        key code 36

        delay 1
        click button "Show All" of group 1 of group 2 of tool bar 1 of window 1
    end tell
end tell

The delay command isn't necessary, it's just so you can visually confirm your choice. It would be nicer to "set" the value rather than doing keyboard tricks, but at least it does the job.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!