10

I am wondering if there is a way to switch the scroll direction on a mac/osx (yosemite) programatically. If so, I want to assign some sort of script or command to a key combination to switch it quickly. I work on a mac but consistently log into windows computers and my brain wants to scroll the wrong way all the time. I haven't programmed anything on mac, still very new to it, but have done plenty in linux and assume its somewhat similar to bash scripting.

the ultimate end goal would be to have this react to a mouse being plugged in also to change both at the same time if needed, i just think it feels more natural to scroll like a windows pc on a mouse and more natural in the osx way on a trackpad. this is really just for fun and ease of use.

user513951
  • 12,445
  • 7
  • 65
  • 82
wondergoat77
  • 1,765
  • 9
  • 32
  • 60
  • It's a built-in feature that the scroll direction can be set separately for mouse vs. trackpad. It's on the respective pane of System Preferences. – Ken Thomases Jun 10 '15 at 22:04
  • 2
    but how to do it programatically? I want to be able to control both, should have mentioned in my post, will update – wondergoat77 Jun 10 '15 at 22:28
  • As of the release of Mac OS Ventura all of the answers here except one are broken. – user513951 Feb 07 '23 at 15:07
  • I know this might be irrelevant, but I need this shortcut is because I need switch between tracking pad and mouse, and finally find this `https://pilotmoon.com/scrollreverser/`. Hope this help someone who have the same situation like me. – Ben May 08 '23 at 13:44

4 Answers4

7

I found an answer here using the defaults command:

Write with: defaults write -g com.apple.swipescrolldirection -bool FALSE (or TRUE)

Read the current value with: defaults read -g com.apple.swipescrolldirection.

AndrewO
  • 1,590
  • 1
  • 17
  • 24
  • 2
    "then log out and log back in to your account", meh, then I might as well use the UI to get it to take immediate effect. – jturcotte Nov 15 '19 at 12:48
  • for me, terminal does read correctly but writing does not change behaviour or the checkbox in preferences (though it does change the value read). I tried the `kill root cfprefsd` in activity monitor thing too. – clarity Mar 19 '21 at 04:46
  • 2
    this no longer works on Ventura. does anyone know the setting for Ventura? thanks. – morpheus Jan 07 '23 at 00:51
  • @morpheus I've added a new answer that solves this for Ventura. – user513951 Feb 07 '23 at 21:37
3

Here is yet another adaptation of the other answers, HOWEVER:

This is the only solution that will allow you to use a key combo from anywhere to immediately reverse scrolling on Mac OS Ventura, without installing any outside applications.

The trick is to create a "Reverse Scrolling" application yourself using Automator, and then use Automator again to create a Quick Action / Service that launches the app when you press the key combo. You need to do this in order to get around permissions issues with AppleScript.

Do not mess around with defaults write. It will not work.

This solution is tested and confirmed on:

  • Mac OS Ventura v13.2
  • Mac OS Monterey v12.4

The steps below refer to the System Settings app. Prior to Mac OS Ventura, this was called System Preferences.

  1. Launch Automator.
  2. Use Command-N or File > New to create a new document.
  3. Choose Application as the document type.
  4. Add a Run AppleScript action to the workflow.
  5. Enter one of the AppleScripts at the bottom of this answer (see notes). Don't worry if running it at this point gives you permissions errors.
  6. Use Command-S or File > Save As... to save as "Reverse Scrolling" with File Format Application in your Applications directory.
  7. Close the Automator document.
  8. Under System Settings > Security & Privacy > Privacy > Accessibility, add the Reverse Scrolling app (remember, it's in your Applications folder) in the pane that says Allow the apps below to control your computer.
  9. Go back to Automator and create a new document.
  10. This time, choose Quick Action as the document type. This will allow you to add the action to the Services submenu in every application.
  11. Configure the workflow so that it requires no input. The top of the document edit pane should say Workflow receives no input in any application.
  12. Add a Launch Application action to the workflow.
  13. In the application chooser dropdown, select Other....
  14. In the finder window that pops up, navigate to your Applications directory and choose the Reverse Scrolling app.
  15. Use Command-S or File > Save As... to save as "Reverse Scrolling". It's fine to use the same name as before.
  16. Under System Settings > Keyboard > Shortcuts > Services, click Reverse Scrolling (under the General subsection in the right checkbox pane) and Add Shortcut.
  17. Press the key combination you want to use. Note that you'll have to avoid collisions with any other key combination, so choose carefully.

If you follow all these instructions correctly, you should now have a Reverse Scrolling menu item under the Services menu of every application, with your key combo listed next to it.

Test it out by scrolling, observing the scroll direction, pressing the key combo, and repeating the scroll test. Scrolling should have reversed, without any error popups or system requests for permissions.

If anything other than perfect scroll reversal occurs, one of two things has occurred—either you've missed a step, or these instructions have gone out of date.


If you want to undo any of the changes you've wrought, here's how:

  • To get rid of the keyboard shortcut, go back to System Settings > Keyboard > Shortcuts > Services and uncheck the box next to Reverse Scrolling.
  • To get rid of the Service completely, delete the directory Library/Services/"Reverse Scrolling.workflow" (relative to your home directory).
  • To rescind permissions for the app, go back to System Settings > Security & Privacy > Privacy > Accessibility and remove Reverse Scrolling from the list of apps there.
  • To get rid of the app completely, delete it from your Applications directory.

Here are two AppleScripts that I've successfully used to reverse scrolling, provided all permissions are in place. These scripts are incredibly fickle and prone to break between versions of Mac OS. By the time you find this answer, you've probably seen several versions of a script that purports to reverse scrolling (though all of them are plagued with permissions errors, unless you carefully follow the steps I've laid out above). If the one for your OS version stops working, and no one has got around to updating this answer, find a different script, but use the rest of the steps above.


Ventura

Works almost every time.

on run {input, parameters}
    do shell script "open x-apple.systempreferences:com.apple.Trackpad-Settings.extension"
    delay 1.0

    tell application "System Events"
        tell process "System Settings"
            click radio button 2 of tab group 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
            click checkbox "Natural scrolling" of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
            tell application "System Settings" to quit
        end tell
    end tell
    
    return input
end run

Monterey

Works every time.

on run {input, parameters}
    tell application "System Preferences"
        reveal anchor "trackpadTab" of pane id "com.apple.preference.trackpad"
    end tell
    
    tell application "System Events" to tell process "System Preferences"
        click checkbox 1 of tab group 1 of window 0
    end tell
    
    quit application "System Preferences"
    
    return input
end run

Important Final Note

If you ever go back to Automator and edit the Reverse Scrolling app to change the AppleScript, the app will lose the permissions you gave it in step 8 above. You will have to go back to the preference pane from step 8 and fully remove the Reverse Scrolling app (using the button) and add it again.

user513951
  • 12,445
  • 7
  • 65
  • 82
  • 1
    Thanks for this very detailed answer. Will try it and let you know. – morpheus Feb 07 '23 at 23:17
  • 1
    I managed to get it working on Ventura 13.3, but I had to remove the `input, parameters` variables due to some errors, and I had to add another `delay 2.0` at the start of the tell process "System Settings" block. I built the script as an application (can't remember the exact details from years ago), and after allowing some permissions, the app is working again. I can now switch the scroll direction by running "scroller" from spotlight, which is user friendly and fast. – Morgus Lethe Apr 08 '23 at 14:17
  • Nice work! works for me. how to you get to know the exact `click radio button 2 of tab...` of the script - where can one find this information? – user20068036 Aug 16 '23 at 07:39
2

I know I'm performing some necromancy here, but I figured out how to make this work (prior to Mac OS Ventura), at least partially:

  • Create a new Service with Automator
  • Service receives no Input and only runs in Finder (this is important for Permissions reasons, see below)
  • under utilities, add the action "Run AppleScript"
  • use this code
on run {input, parameters}
    
    tell application "System Preferences"
        reveal anchor "trackpadTab" of pane id "com.apple.preference.trackpad"
    end tell
    
    tell application "System Events" to tell process "System Preferences"
        click checkbox 1 of tab group 1 of window 0
    end tell
    
    quit application "System Preferences"
    
    return input
end run
  • Save
  • Go to System Preferences -> Security & Privacy -> Privacy -> Accessibility
  • Add Finder.app (/System/Library/CoreServices/Finder.app)
  • Go to System Preferences -> Keyboard -> Shortcuts -> Services
  • Add a keyboard shortcut to your service
  • ???
  • Profit
user513951
  • 12,445
  • 7
  • 65
  • 82
poddus
  • 29
  • 4
  • Was excited about this solution, but not sure that it works in the latest version. I like where you are headed though. I tried to implement it and will update if I find anything that get's it working. – Andrea Thacker Aug 04 '20 at 20:03
  • Loved this solution, yet it didn't worked at first and had to modify a bit. There were couple issues: 1. Whatever is running this script requires Accessibility permission to click the checkbox. 2. index of window was incorrect, maybe it was UI update with some OS update, yet i fixed it and now it works. Though i'm just using Apple's official Apple Script editor to run this. – Lukas Liesis Oct 17 '20 at 12:23
  • The idea is very clever. Unfortunately, the script here is broken since the release of Mac OS Ventura. – user513951 Feb 07 '23 at 15:09
2

Note:

This works for older version of Macs. You may look into the code in linked repo to adapt for newer though. It worked with v12, not with v13.

I believe many just have the same issue - connect the mouse and want to toggle scroll direction w/o going to the settings.

I actually went an extra step and created guide on this

https://medium.com/@liesislukas/how-to-set-scroll-direction-independently-for-mouse-and-trackpad-on-macbook-66bb6812678

You can get the zipped application on my github: https://github.com/liesislukas/apple-scroll-direction-auto-toggle or just use link from the article i posted on medium with all the screenshot and documentation.

To clarify, yes I used code from another post on this thread, yet code there was broken. I fixed the code in post above and just packed it up to the application, added nice icon so now it can be added to dock to toggle the mouse direction. Thanks @poddus for giving hint to use the Apple Script :)

enter image description here

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
  • This is broken in Mac OS Ventura. – user513951 Feb 07 '23 at 15:08
  • @user513951 do you have info why? consider sending PR to git repo – Lukas Liesis Feb 08 '23 at 19:18
  • They've redone System Preferences in Ventura. It's called System Settings now, the panes are different in many ways, and the old ways of getting at the panes in AppleScript (e.g. "reveal anchor") don't work anymore. I googled around and found a way to do it that works _some_ of the time; it's in the answer I wrote. It's going to be up to you whether you decide to support older versions of Mac OS or just modify your app to support the latest, but if you update your app to support Ventura I'll flip my vote. – user513951 Feb 08 '23 at 21:12
  • 1
    @user513951 thanks for pointing out. I added a note to answer itself that this does not work with v13. I use magic mouse now and lost interest in such app, won't support v13 but if anyone would look for a way out, same code may be still useful. – Lukas Liesis Feb 09 '23 at 11:21