Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically invert screen colors on Mac

Tags:

macos

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.

like image 841
Elliott Slaughter Avatar asked Jun 28 '11 18:06

Elliott Slaughter


2 Answers

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...

like image 94
michiakig Avatar answered Oct 23 '22 03:10

michiakig


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
like image 39
Miguel Avatar answered Oct 23 '22 04:10

Miguel