MacOS – Automate change in settings upon plugging mouse in

automationautomatormacosmousesettings

I am using a MacBook Air running Sierra.

I like to use different mouse settings with my touchpad, and my USB mouse. To facilitate this, every time I plug my mouse in, I have to go to System Preferences and change the mouse settings (reverse scroll direction and adjust sensitivity). Is there any way by which I can automate this change (maybe using automator) in settings to occur automatically every time I plug in the USB mouse?

Best Answer

Under macOS Sierra, if you have only a normal USB mouse, not one that comes with it's own software and is multi-function with additional buttons, etc., then in System Preferences > Mouse, you have the following settings that can be changed:

  • Scroll direction: natural checkbox
  • Tracking speed slider
  • Scrolling Speed slider
  • Double-Click speed slider

And two additional settings under Mouse that will not be addressed, for two reasons, the Primary mouse button: radio buttons, if other then the default of Left, is in all likelihood already set and wouldn't necessarily change because one wants to change the Scroll direction: natural checkbox, and since the OP stated USB mouse, I'll assume is not using Bluetooth and can ignore the Setup Bluetooth Mouse... button.

This answer is going to use AppleScript to get the before and after settings for the value of the aforementioned listed sliders to be used in the AppleScript code that follows after this code.

Open Script Editor and create a new document, then add the following code:

tell application "System Preferences"
    set current pane to pane id "com.apple.preference.mouse"
    --delay 0.5 -- # If necessary, uncomment 'delay' command and set its value in seconds and or decimal fractions there of.
    tell application "System Events"
        log "Scrolling speed is set to: " & value in (get properties of slider 1 of front window of application process "System Preferences")
        log "Double-Click speed is set to: " & value in (get properties of slider 2 of front window of application process "System Preferences")
        log "Tracking speed is set to: " & value in (get properties of slider 3 of front window of application process "System Preferences")
    end tell
end tell

There will be two difference states of the Scroll direction: natural checkbox in System Preferences > Mouse, checked and unchecked and the change in slider settings between the two states.

The code above will be run once to get the current value settings of the sliders. Then make the setting changes that you want to the sliders for the opposite state of what it was when you gathered the values of the first settings for the first state. Then run the code again to get the differential settings.

You will then plug in the values, gathered above, in the code below. You can comment out or remove any slider that's not going to be changed by the code below.

The comments in the code below should be adequate, however, don't hesitate to ask it you need anything explained.

I saved the following AppleScript code in Script Editor as an application named Toggle Mouse Settings in the Applications folder.

When run, it toggles the state of the Scroll direction: natural checkbox between checked and unchecked along with the slider settings under each state.

AppleScript code for the Toggle Mouse Settings app:

on run
    try
        tell application "System Preferences"
            set current pane to pane id "com.apple.preference.mouse"
            -- delay 0.5    -- # If necessary, uncomment 'delay' command and set its value in seconds and or decimal fractions there of.
            tell application "System Events"
                set isChecked to get value of checkbox 1 of front window of application process "System Preferences" as boolean
                if isChecked then
                    --  # The 'Scroll direction: Natural' checkbox is checked, uncheck it.
                    set cbValue to value of (click checkbox 1 of front window of application process "System Preferences") as boolean
                    --  # Scrolling Speed - Valid Values: 0.0 to 8.0
                    set value of slider 1 of front window of application process "System Preferences" to 6.0
                    --  # Double-Click Speed - Valid Values: 0.0 to 10.0
                    set value of slider 2 of front window of application process "System Preferences" to 8.0
                    --  # Tracking Speed - Valid Values: 0.0 to 9.0
                    set value of slider 3 of front window of application process "System Preferences" to 6.0
                else
                    --  # The 'Scroll direction: Natural' checkbox is not checked, check it.
                    set cbValue to value of (click checkbox 1 of front window of application process "System Preferences") as boolean
                    --  # Scrolling Speed - Valid Values: 0.0 to 8.0
                    set value of slider 1 of front window of application process "System Preferences" to 4.5
                    --  # Double-Click Speed - Valid Values: 0.0 to 10.0
                    set value of slider 2 of front window of application process "System Preferences" to 8.0
                    --  # Tracking Speed - Valid Values: 0.0 to 9.0
                    set value of slider 3 of front window of application process "System Preferences" to 4.0
                end if
            end tell
            tell me
                if cbValue then
                    display notification "Natural scrolling is now active." with title (name of me)
                else
                    display notification "Natural scrolling is no longer active." with title (name of me)
                end if
            end tell
            quit
        end tell
    on error eStr number eNum
        activate
        display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "Toggle Mouse Settings" with icon caution
        tell application "System Preferences" to quit
        return
    end try
end run

Note: Before running the Toggle Mouse Settings app, you'll need to add it to: System Preferences > Security & Privacy > Privacy > Accessibility


There is an app called EventScripts that could be used to trigger when a USB device is plugged in and or removed. I have not used it before, just aware it exists.

There is also an app called Scroll Reverser. I have not used it before, just aware it exists.