Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Dynamically change background colors in iTerm Apps
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:
  • Use AppleScript to tell the current terminal to change background color
  • Use an alias/function in bash to invoke the AppleScript each time su is run
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.
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[12,582 views]  

Dynamically change background colors in iTerm | 3 comments | Create New Account
Click here to return to the 'Dynamically change background colors in iTerm' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Dynamically change background colors in iTerm
Authored by: raider on Dec 22, '04 12:11:31PM
The hint/discussion about doing this for Terminal can be found here. The following discussion improved the process quite a bit so you might want to read through it.... :)

[ Reply to This | # ]
Dynamically change background colors in iTerm
Authored by: kholburn on Dec 22, '04 10:33:42PM
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 not sure I understand what you're trying to say here. Isn't the point of the script to change the colour of an iTerm?

None-the-less using exec in a script means the exec line will take over the shell running the script and any lines in the script after the exec statement will not be run. Since you do it in brackets it creates a subshell and then takes over from the subshell. The brackets and the exec are therefore superfluous.

Maybe should look like this:
su() {
  setBackground "{15000,0,0}"
  su $*
  setBackground "{0,0,0}"
}

If the brackets and backgrounding are because apple talk is so slow then maybe:
su() {
  setBackground "{15000,0,0}" & 
  su $*
  setBackground "{0,0,0}" &
}

The problem with this is that the two background tasks could conflict in peculiar ways like the order may be wrong.


[ Reply to This | # ]
Dynamically change background colors in iTerm
Authored by: spirp on Dec 27, '04 07:37:25AM

First, yes, I want to change the background color of the current iTerm when I su in an iTerm, but not when running Terminal (unfortunatley I'm not aware of a method to determine wether I'm doind this in an iTerm or a Terminal (except maybe sourcing a separate script from iTerms bookmarks-thingie).

Secondly, I know all the brackets and stuff are quite ugly, but that was the only way I could get it to do what I wanted (that is, supressing job control-output and stuff). Also, if I didn't exec the 'su $*', it would lock up after the first setBackground-function call. Didn't bother to look in to it any further, so that's why it's an ugly hack ;) It works fine for me though :)



[ Reply to This | # ]