Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I use mouse with left-hand. My system is shared with other people who are right handed. So, I need frequently to switch mouse button.

I would like to know how to swap mouse button using AppleScript? I am using default mouse come with my iMac machine.

share|improve this question

1 Answer 1

I only tested this with the normal version of the preference pane, and it doesn't probably work if you have a Magic Mouse:

tell application "System Preferences"
    reveal pane "com.apple.preference.mouse"
end tell
tell application "System Events" to tell process "System Preferences"
    tell radio group 1 of window 1
        if value of radio button 1 is 1 then
            click radio button 2
        else
            click radio button 1
        end if
    end tell
end tell

The setting is stored in this key, but I don't know any way to apply changes to it immediately:

defaults -currentHost read -g com.apple.mouse.swapLeftRightButton

You could also use KeyRemap4MacBook:

<?xml version="1.0"?>
<root>
<item>
<name>test</name>
<identifier>test</identifier>
<autogen>__KeyToKey__ KeyCode::F1, VK_COMMAND | ModifierFlag::NONE, KeyCode::VK_CONFIG_TOGGLE_swapbuttons</autogen>
</item>
<item>
<name>swapbuttons</name>
<identifier vk_config="true">swapbuttons</identifier>
<autogen>__PointingButtonToPointingButton__ PointingButton::LEFT, PointingButton::RIGHT</autogen>
<autogen>__PointingButtonToPointingButton__ PointingButton::RIGHT, PointingButton::LEFT</autogen>
</item>
</root>

See the source for the key code values and predefined settings.

share|improve this answer
    
Thanks for the reply. I got the error error "System Events got an error: Access for assistive devices is disabled." number -1719 from window 1 of process "System Preferences" –  dbasic Apr 25 '13 at 7:10
    
You just have to enable access for assistive devices in the accessibility preference pane. –  Lri Apr 25 '13 at 7:14
    
Now I am getting "System Events got an error: Can’t get radio group 1 of window 1 of process \"System Preferences\". Invalid index." number -1719 from radio group 1 of window 1 of process "System Preferences" –  dbasic Apr 25 '13 at 7:16
    
It has drop down under Secondary Click in Point & Click tag. –  dbasic Apr 25 '13 at 7:17
    
As mentioned in your answer, my mouse is Magic mouse and it is not working for it. –  dbasic Apr 25 '13 at 8:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.