Colorful Terminal

I spend a lot of time SSHing to various boxes. This handy little script will set the Terminal to a certain color when I start a session and set it back to white when I'm done. There's nothing worse than being on one box and thinking you're on another. Also, this makes the minimized icons colorful.

First of all, we need to learn how the Terminal refers to colors. You can just use basic words like 'white', 'red', etc., but those colors tend to be pretty intense. It's better to use a light tint of a color, so if you want to get custom,

  1. Launch Terminal
  2. Choose 'Window Settings...' from the 'Terminal' menu
  3. Choose 'Color' from the dropdown menu
  4. Click the white box next to 'Background'
  5. Pick a color you like
  6. Copy and paste this line into Terminal exactly as shown:
    osascript -e 'tell application "Terminal" to set x to the background color of the front window'
  7. Jaguar might give an error but you'll still get the output you want. Panther and newer will just give output. What you're looking for is something like this:
    -1, -16950, 4296
  8. Select and copy those numbers.

Now, we need to write a little SSH script, Here's mine, called 'sshweb,' to get to a website I admin.

#!/bin/bash
osascript -e 'tell app "Terminal" to set background color of first window to {15601, -1, 31913}'
ssh jsmith@example.org
osascript -e 'tell app "Terminal" to set background color of first window to "white"'


Line one is the shebang, of course. Line two sets the Terminal to the color I want. Line 3 actually starts the connection. Once you're logged into the remote box, the script is still running as far as the local machine is concerned. The Terminal will stay that color for as long as you're logged in. Once you log out of the remote box, the local script resumes, sets the Terminal back to white (line 4), and exits. Pretty sweet, eh? Use this and you'll always know at a glance where you are.

Update for 10.5 and 10.6: It seems that Apple has finally gotten smart about color values. It is now three numbers, apparently from 0 to 65k, and apparently just R, G, and B. To get the murky green that used to be "{18712, -25207, 21758}" I now use "{20000, 40000, 20000}".

Another udpate: when running on a 64-bit CPU, add "arch -i386" before "osascript", like this:
arch -i386 osascript -e 'tell app "Terminal" to set background color of first window to {48000, 48000, 48000}'
Thanks http://forums.adobe.com/thread/486208 !

Bonus tip: and speaking of colors, Mac OS X by default gives monochromatic responses to commands like "ls". To make the actual output colorful, put
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad

into your ~/.bash_profile. (Thanks http://theappleblog.com/2009/03/26/terminal-tips-using-the-command-line-with-style/ !)

sample