Elliott Slaughter
Elliott Slaughter

Reputation: 1555

How to programmatically invert screen colors on Mac

How can I write a script to run on Mac that inverts screen colors?

Similar to How to programmatically invert screen colors in Linux, but unfortunately xcalib works on Windows and Linux but not Mac as far as I can tell.

EDIT: I have a partial solution. I found a way dump all of my settings, before and after inverting screen colors:

$ mkdir before && mkdir after && cd before
$ for d in $(defaults domains | sed 's/,//g'); do defaults read $d > $d; done
$ cd ../after
$ # System Preferences > Universal Access > Display > White on Black
$ for d in $(defaults domains | sed 's/,//g'); do defaults read $d > $d; done
$ diff -r before after
diff -r before/com.apple.CoreGraphics after/com.apple.CoreGraphics
3c3
<     DisplayUseInvertedPolarity = 0;
---
>     DisplayUseInvertedPolarity = 1;
diff -r before/com.apple.universalaccess after/com.apple.universalaccess
5c5
<     whiteOnBlack = 0;
---
>     whiteOnBlack = 1;

So now I know what settings are responsible for screen inversion. But when I try the obvious,

$ defaults write com.apple.universalaccess whiteOnBlack -int 1
$ defaults write com.apple.CoreGraphics DisplayUseInvertedPolarity -int 1

Nothing happens. Presumably whatever programs use these values need to be told to reload them (since the examples from e.g. dotfiles need to kill Finder to take effect). But I'm not sure what apps those would be, or whether this is the correct solution anyway.

Upvotes: 6

Views: 3986

Answers (4)

pointum
pointum

Reputation: 3177

On macOS Monterey or newer it’s now possible to toggle Smart and Classic Invert using the new Shortcuts app. Search for "Invert" in the Action list when creating a new shortcut.

Upvotes: 1

Hugues BR
Hugues BR

Reputation: 2258

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

Upvotes: 1

Miguel
Miguel

Reputation: 11

You can try to perfect this snippet that returns the current status with GUI scripting. The value seems to be read-only and trying to set it to 1 or 0 fails.

tell application "System Preferences"
    launch
    set current pane to pane "com.apple.preference.universalaccess"
    tell application "System Events"
        return value of checkbox "Invert colors" of window 1 of process "System Preferences"
    end tell
    quit
end

Upvotes: 1

okonomichiyaki
okonomichiyaki

Reputation: 8485

This snippet of Apple Script will do it:

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

This uses the Control-Option-Cmd-8 shortcut (keycode 28 is the number 8). You'll have to figure out how to call it from whatever you need...

Upvotes: 4

Related Questions