Apple Script to set Primary Button, Secondary Button and other properties for Mouse Preference
Can anyone suggest me:
How to set Primary Button, Secondary Button and other properties of Mouse Preference via AppleScript?
I have now tried this, script:
tell application "System Preferences"
activate
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "Mouse" of menu "View" of menu bar 1
tell window "Mouse"
set value of pop up button 1 to "Primary Button"
end tell
end tell
end tell
but it is giving this error message:
System Events got an error: Can't get pop up button 1 of window "Mouse" of process "System Preferences". Invalid 开发者_开发问答 index.
Can anyone suggest me where I may be wrong or some better trick to implement it?
Thanks,
Miraaj
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.
精彩评论