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

Switch Terminal colors per user Apps
After the recent post about switching iTerm's colors, and reading the post about dynamically switching to random colors in Terminal, I decided to look for a way to switch to pre-determined colors in Terminal per user. I use this mainly to visually see if I happen to be running as root, but it can be used for any user.

This first part of the process are these commands that need to go into /etc/bashrc. This file is looked at by bash while setting up the shell (it's also looked at by the sh shell as well). It has two different parts -- the first part looks to see if you're running the Terminal application, and if you are, it runs the script to set the colors for that user. The second part is a function that replaces su. This function checks if you are running Terminal, and if you are it sets some environment variables, and uses su. Once the su'd user exits, it sets the Terminal colors back to the original user's colors.

The next step is the following userColors.scpt Applescript template. This is the colors I use for root on my machine. I'd just like to note that the reader will need to create a script for their normal user, as well for the colors to revert back to normal. This one would be saved as rootColors.scpt in /Library -> Scripts -> Terminal. The script, when run, will change the color of the frontmost window in Terminal.
tell application "Terminal"
	set backgroundColor to {25205, 297, 0, -13108}
	set textColor to {-9787, -9787, -9787}
	set boldColor to {-15421, -28366, 0}
	set cursColor to {-22835, -22835, -22835}
	set the background color of window 1 to backgroundColor
	set the normal text color of window 1 to textColor
	set the bold text color of window 1 to boldColor
	set the cursor color of window 1 to cursColor
end tell
This next AppleScript is the one I use to get the colors from the frontmost window in Terminal. I use this to get the colors to plop into the above script. Note that if you run this via osascript in the Terminal. Note that it won't put the curly brackets around the numbers, but those are definitely needed in the above script.
tell application "Terminal"
	set backgroundColor to background color of window 1
	set cursColor to cursor color of window 1
	set normalColor to normal text color of window 1
	set boldColor to bold text color of window 1
end tell

set theList to {"background color", backgroundColor, "cursor color", 
 cursColor, "normal text color", normalColor, "bold text color", boldColor}

return theList
When you have this all set up and working, you just use su in the Terminal to switch to another user, and the colors of the terminal will change accordingly. Once you log out, the colors are changed back (assuming that the original user has a *Colors.scpt made for them). I've found this very useful -- I use the terminal a lot, and often log in as root, but also usually forget to log out. Now, I can visually see if I'm logged in and will remember to log out. Some caveats for this:
  • If you use sudo (i.e. sudo su), it uses the normal su not this replacement function. The colors will switch, but will not switch back when you exit. The replacement function can be edited to sudo /usr/bin/su ${loginName} instead of bash -c .... if you wish to use sudo. Note that doing this, though, will prevent non-admin users from using this function at all.
  • There's a slight pause when osascript is being called to change the colors. Unfortunately, the quicker defaults write .... command can't be used because a new window isn't being opened.
[robg adds: I haven't tested this one...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[8,063 views]  

Switch Terminal colors per user | 8 comments | Create New Account
Click here to return to the 'Switch Terminal colors per user' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Totally Unsecure
Authored by: rmiller021 on Dec 28, '04 12:22:56PM
This is fine if you trust your users.
What happens if someone adds do shell script "/bin/rm -f" to your apple script?

[ Reply to This | # ]
trust relationships
Authored by: gatorparrots on Dec 29, '04 12:37:06AM
You give shell accounts to untrustworthy users? /dev/null is my shell assignment for such accounts.

[ Reply to This | # ]
Switch Terminal colors per user
Authored by: gshenaut on Dec 28, '04 01:35:10PM
It's nowhere near as flashy, but what I do is to put this line into the start-up for all my machines (I like to use /etc/profile for system-wide things like this, but ~/.XXXrc is fine too):
PS1="\$LOGNAME@`hostname -s`\$ " ; export PS1
This tells you who you are and what machine you are talking to. Sadly, it's not color-coded, though.

Greg Shenaut

[ Reply to This | # ]

Switch Terminal colors per user
Authored by: koncept on Dec 28, '04 03:22:17PM
This is my prompt.. Aqua coloured..

// appears as [user@host:/path]$ 
declare -x PS1="\[\033[0;36m\][\u@\h:\w]$\[\033[0m\] "


[ Reply to This | # ]
Switch Terminal colors per user
Authored by: gshenaut on Dec 28, '04 05:41:31PM

Yeah, but the hard part is to change the color when changing the user, and then to restore the original color when changing back to the previous user.

Greg Shenaut



[ Reply to This | # ]
Switch Terminal colors per user
Authored by: guybrush on Dec 29, '04 05:48:40AM
This is mine:

[guybrush@powerbook guybrush]$ echo $PS1
\[\033[35m\][\[\033[36;1m\]\u\[\033[0;36m\]@\[\033[1m\]\h \[\033[33m\]\W\[\033[0;35m\]]\[\033[0m\]$
looks best on a black background Terminal ;)

[ Reply to This | # ]
Switch Terminal colors per user
Authored by: Darkshadow on Dec 29, '04 08:33:22PM

Hmm, if y'all are using bash (or sh), you can use \e instead of \033.

This is how my prompt works... output's a bit different, but then I'm a bit different. ;)

\[\e[1m\]\[\e[37m\]<\[\e[36m\]\u\[\e[37m\]://\[\e[33m\]\h/\W.htm\[\e[37m\]>\[\e[0m\] \[\e[37m\]\$

(output as <user://host/last_component_cwd.htm> $ )



[ Reply to This | # ]
Switch Terminal colors per user
Authored by: Thomas Riise on May 19, '05 05:26:24AM
I also have a quite simple env defined in .profile for various users - with colors and everything:

Example:
PS1='\e[0;36m${PWD}:\e[m\] '
export PS1


e.g. RED is smart when you're logged in as root etc...



---
An Apple a day keeps Microsoft away...

[ Reply to This | # ]