Change mouse speed in terminal without restarting

command linemouse

I used this link https://paulminors.com/how-to-speed-up-mouse-tracking-on-mac/ which has a bash line that updates your mouse speed (beyond the default maximum):

defaults write -g com.apple.mouse.scaling your_mouse_speed

However, in order to make it work, you need to restart, which is kind of a pain. I assume what it's doing is really to change the preferences pane or plist? Thus, there should be a way to reboot just the mouse or the process that draws from the mouse settings. For example, if you change your speech synthesis .plist, you just reboot the speech synthesis core using a terminal command like killall SpeechSynthesisServer

Is there a way to do this with the mouse settings? I can obviously restart, but I'd rather not since I want this to be a quick thing.

Best Answer

I couldn't find a way to do it with terminal alone so I had to create an Apple Script.

I found a script here: https://stackoverflow.com/questions/1680775/setting-the-mouse-tracking-speed-via-applescript

However, this did not work for me in Mojave so I had change it a littler. I also changed it so that it accepts a parameter which it will then forward to the tracking speed slider.

on run (trackingValue)
--Open and activate System Preferences
tell application "System Preferences" to activate

--Attempt to change settings using System Events
tell application "System Events"
    tell process "System Preferences"
        try
            delay 1
            --Open the "Mouse" pane
            click menu item "Mouse" of menu "View" of menu bar 1
            delay 0.5
            tell window "Mouse"
                tell slider "Tracking speed" of tab group 1
                    set value to round of trackingValue rounding down
                end tell
            end tell
        on error theError
            --An error occured
            display dialog ("Sorry, an error occured while altering Mouse settings:" & return & theError) buttons "OK" default button "OK"
        end try
    end tell
end tell

tell application "System Preferences" to quit
end run

You can then run this via terminal by calling:

osascript /<SCRIPT_LOCATION>/<NAME_OF_SCRIPT>.scpt <TRACKING_SPEED_VALUE>

You will need to give your Terminal accessibility access for this to work.