An AppleScript to Invert the Display in Grayscale (And Another to Revert) ¬

2010-12-17

I’ve been doing a lot of early morning coding in bed, and have found that the only way I can do so without killing my eyes is to select “Use grayscale” as well as “White on Black” in the Universal Access preferences pane in System Preferences. This gives me a far more comfortable, inverted grayscale display. Unfortunately, it’s annoying and painful to initially set when my eyes are at their most sensitive.

I’ve modified a script posted on Apple Discussions to enable grayscale to also invert the display1 and saved it as a run-only application named Invert Display.app:

tell application "System Preferences" to activate
delay 1
tell application "System Events"
	tell process "System Preferences"
		click the menu item "Universal Access" of the menu "View" of menu bar 1
		click the radio button "Seeing" of the first tab group of window "Universal Access"
		if value of (checkbox "Use grayscale" of tab group 1 of window "Universal Access") is 0 then
			click the checkbox "Use grayscale" of tab group 1 of window "Universal Access"
		end if
		-- click the radio button "White on Black" of tab group 1 of window "Universal Access"
	end tell
	key code 28 using {control down, option down, command down}
end tell
tell application "System Preferences" to quit

Naturally, I also created another to restore the settings and named it Restore Display.app:

tell application "System Preferences" to activate
delay 1
tell application "System Events"
	tell process "System Preferences"
		click the menu item "Universal Access" of the menu "View" of menu bar 1
		click the radio button "Seeing" of the first tab group of window "Universal Access"
		if value of (checkbox "Use grayscale" of tab group 1 of window "Universal Access") is 1 then
			click the checkbox "Use grayscale" of tab group 1 of window "Universal Access"
		end if
		-- click the radio button "Black on White" of tab group 1 of window "Universal Access"
	end tell
	key code 28 using {control down, option down, command down}
end tell
tell application "System Preferences" to quit

I just launch them from Spotlight as needed. It’s a little distracting to see System Preferences launch and do it’s thing, but far easier than doing it myself when my eyes are being seared out of their sockets.

1 – Note that I’ve left the line for selecting the “White on Black” radio button in the script, but commented out, in case someone can catch the reason it throws an error. Since I’m in the preference pane anyway, it seems silly to use key code, plus I’d rather be able to verify it’s actually selected (like I do with the checkbox) so I’m not blindly toggling.

  Textile help