Swap Mouse Button using AppleScript

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