AppleScript: Reverse screen colors on a Mac and keep a black background in Terminal 11

Posted by ryan Mon, 12 Nov 2007 06:21:32 GMT

Sometimes, it's such a hassle to get up and turn on a light when I'm in the middle of coding. Other times, I don't have control over the lighting, like in a dim pub or cafe. In dim lighting, it's a strain on my eyes to be looking at black text on a white background. I've tried dimming the brightness, but that decreases the contrast, which is also a strain on my eyes. Then, I discovered Control+Option+Command+8, which is a keyboard shortcut to the "Reverse black and white" Universal Access feature. Contrary to it's name, it does more than reverse black and white, it reverses all of the colors on the screen. This, however, is acceptable and requires only a slight mental adjustment for me. Now I use a nice black-on-white color scheme in TextMate for coding, and when I'm in dim lighting, I simply reverse the colors and it's white-on-black, still full-contrast. I'm happy, except for one thing.

I often have a number of Terminal windows open when I'm working, and I open and close them frequently. Although I'm happy with a black-on-white color scheme for TextMate, I can't tolerate anything but white-on-black for my normal Terminal windows. So, what happens when I throw the screen colors in reverse is that all of my lovely white-on-black Terminal windows go to black-on-white. Unacceptable! Thus began my first foray into AppleScript.

After referencing here, here, and here, along with the Terminal "dictionary", I came up with this:

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

tell application "Terminal"
  if default settings is equal to settings set "White on Black" then
    set settingsSet to "Black on White"
  else if default settings is equal to settings set "Black on White" then
    set settingsSet to "White on Black"
  end if
  set default settings to settings set settingsSet
  repeat with w in every window
    set current settings of w to settings set settingsSet
  end repeat
end tell

This script first invokes the Control+Option+Command+8 keyboard shortcut to reverse the screen colors. It then toggles the default "settings set" for Terminal to one of two pre-defined sets I've created: "White on Black" and "Black on White". Hopefully the names are self-explanatory. Then, the script iterates over all currently-open Terminal windows and changes their current "settings set" to whatever the new default is.

Pretty simple. I named the script "Reverse Screen Colors" and dropped it in /Users/[user account]/Library/Scripts. I invoke it via the keyboard with QuickSilver.

  1. Dara almost 2 years later:

    Thanks! This is exactly what I was looking for. I only discovered the invert color command today. Before that, I've been using a program called "Shades" to bring the brightness down even more than the lowest settings.

  2. pizza over 2 years later:

    thanks ,pizza,Good job.What I most enjoy is mini cooper,thanks,My wedding What next?

  3. playhouse outdoor plastic almost 5 years later:

    What's up i am kavin, its my first time to commenting anywhere, when i read this post i thought i could also create comment due to this sensible piece of writing.

  4. sites.google.com about 5 years later:

    Excellent blog here! Also your web site loads up very fast! What host are you using? Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol

  5. sites.google.com about 5 years later:

    Excellent blog here! Also your web site loads up very fast! What host are you using? Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol

  6. vendositoweb.com over 5 years later:

    Hi there, I do think your website could possibly be having browser compatibility problems.

    When I look at your blog in Safari, it looks fine however when opening in Internet Explorer, it has some overlapping issues. I simply wanted to provide you with a quick heads up! Besides that, wonderful site!

  7. vendositoweb.com over 5 years later:

    Hi there, I do think your website could possibly be having browser compatibility problems. When I look at your blog in Safari, it looks fine however when opening in Internet Explorer, it has some overlapping issues. I simply wanted to provide you with a quick heads up! Besides that, wonderful site!

  8. http://ocnydentistreviews.com over 5 years later:

    Hurrah! Finally I got a weblog from where I be capable of genuinely obtain valuable data concerning my study and knowledge.

  9. http://ocnydentistreviews.com over 5 years later:

    Hurrah! Finally I got a weblog from where I be capable of genuinely obtain valuable data concerning my study and knowledge.

  10. asics tiger over 5 years later:

    Hurrah! Finally I got a weblog from where I be capable of genuinely obtain valuable data concerning my study and knowledge.

  11. ushiti gobleni over 5 years later:

    An outstanding share! I've just forwarded this onto a colleague who was doing a little homework on this. And he actually ordered me dinner because I found it for him... lol. So let me reword this.... Thanks for the meal!! But yeah, thanx for spending the time to discuss this matter here on your site.