How can I script inverting screen colors?

And, more generally, how can I change accessibility settings via the command line?

Worth pointing out that I'm pretty comfortable with bash already- coming from a systems administration / *nix background- but I just don't know which files to edit or tools to use.

To clarify, I am not asking this question, the resolution of which modifies a setting under Security and Privacy. What I want to do is change a setting directly under Accessibility. I'll grant the title of that question is misleadingly similar, but the content is entirely divorced from my intent.


  • If it's okay to give Terminal.app permanent access to Accessibility:

    1. Open System Preferences
    2. Go to Security & Privacy section
    3. In Privacy tab, click on the lock in bottom left and enter your password to enable editing
    4. Select Accessibility from left side list and then check Terminal checkbox in right side list (if there is no Terminal checkbox here you might run step 5 once and then back to this step)
    5. Open Terminal and run this line (it uses AppleScript):

       osascript -e "tell application \"System Preferences\"" -e "activate" -e "reveal anchor \"Seeing_Display\" of pane id \"com.apple.preference.universalaccess\"" -e "tell application \"System Events\" to tell process \"System Preferences\"" -e "click the checkbox \"Invert colors\" of window \"Accessibility\"" -e "end tell" -e "quit" -e "end tell"
      

      repeating previous steps for the future is not needed.

  • If it's not okay to give direct access:

    1. Open System Preferences
    2. Go to Keyboard section
    3. In Shortcuts tab, Select Accessibility from left side list and then check Invert colors in right side list (Default shortcut is ctrl+alt+cmd+8).
    4. Open Terminal and run this line (it uses AppleScript):

       osascript -e "tell application \"System Events\"" -e "key code 28 using {control down, option down, command down}" -e "end tell"
      

      repeating previous steps for the future is not needed.

You can write those codes in external script files and run with osascript invertColors.scpt


to invert: printf '\e[?5h'


to un-invert: printf '\e[?5l'