Dynamically change background colors in iTerm

Dec 22, '04 08:29:00AM

Contributed by: spirp

I've always liked the idea of having a different background color for terminals where I'm logged in as root. I've always used serveral terminals for this, but it gets quite tiresome to open up a new .term-file or similar, just to get another background color, so I came up with this solution:

I've only done this with iTerm (which is my terminal of choice), but Terminal should be able to do something similar. In ~/.bash_profile, I've added the following lines:
setBackground() {
  osascript -e "tell application \"iTerm\"
    set current_terminal to (current terminal)
    tell current_terminal
      set current_session to (current session)
      tell current_session
        set background color to $1
      end tell
    end tell
  end tell"
}

su() {
  ( setBackground "{15000,0,0}" & )
  ( exec su $* )
  ( setBackground "{0,0,0}" & )
}
The first function tells iTerm to change colors, and the second "replaces" su, so that this function is executed whener I type su at the prompt. Unfortunately, there are some unwanted behaviours with this, such as if you try to run su from Terminal with the script above, it will change the color of an iTerm. I'm sure there are other bugs as well, but I haven't noticed any yet.

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20041218131532551