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.
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.
•
[8,094 views]

