MacOS – How to set up a system-wide key combination to toggle a specific System Preferences setting (“Tap to Click”)

applescriptkeyboardmacossystem-prefstrackpad

I want to be able to toggle the "Tap to Click" setting under "Trackpad" > "Point & Click" in the System Settings app with a key combination from within any application.

I looked at "Keyboard" > "Shortcuts", but the options here look pretty limited. "App Shortcuts" seem to be the closest thing — it appears you can bind a key to an option in pull-down menus on the top menu bar, but if there's a way to bind a key to a toggle in the System Settings app pane, it's not clear to me.

Perhaps this is something that's better done by AppleScript (or system-hooked JavaScript)? Or triggering a command-line option?

Best Answer

Since you didn't say what version of macOS you are running, here is the AppleScript code I'd use to toggle the target checkbox under macOS High Sierra. It may need to be modified for macOS Mojave and later.

Also note that when assigning a global keyboard shortcut, it can not be one already assigned to any app that has focus when the keyboard shortcut is pressed.

You can use this code in a Run AppleScript action in an Automator Service.1

  • 1 In macOS Mojave, and later, an Automator Service is called a Quick Action. There are also other minor nomenclature differences but they should be more obvious when comparing them to pre-macOS Mojave Automator workflows.

The keyboard shortcut can be assigned in: System Preferences > Keyboard > Shortcuts > Services

Example AppleScript code:

if running of application "System Preferences" then
    try
        tell application "System Preferences" to quit
    on error
        do shell script "killall 'System Preferences'"
    end try
end if

repeat while running of application "System Preferences" is true
    delay 0.1
end repeat

tell application "System Preferences" to ¬
    reveal pane id "com.apple.preference.trackpad"

tell application "System Events" to tell application process "System Preferences"
    repeat while not (exists window "Trackpad")
        delay 0.1
    end repeat
    tell tab group 1 of window "Trackpad"
        click radio button "Point & Click"
        repeat while value of radio button "Point & Click" is equal to 0
            delay 0.01
        end repeat
        click checkbox 3
    end tell
end tell

quit application "System Preferences"

Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.