Random Color Terminal
November 2nd, 2006Erik Barzeski stumbled on a cool trick with Terminal’s scriptability, allowing him to save presets for his favorite color schemes in AppleScript. His approach is especially brilliant since he’s using FastScripts to trigger the scripts!
This gave me a little whimsy of an idea, that it would be cool to have a script on hand to change the color of the frontmost terminal to some random color scheme.
Random Color Terminal does just that. It sure comes up with some ugly schemes, but the good news is that with a Terminal-specific shortcut assigned to the script, I can rip through random choices until I see something I like. I even discovered a little algorithm online to determine whether black or white text is best for the given background color.
Speaking of Terminal and AppleScript, here’s a little quickie that I really appreciate in Terminal:
tell application "Terminal" try do script "cd ~/" in window 1 on error do script "cd ~/" end try end tell
It shouldn’t be hard for you to recognize this as “go to my home directory.” So what’s the big deal? Well I set it as a FastScripts shortcut with the key combination “Cmd-Shift-H.” The same combination as the Finder uses for the same action. So now whether I’m sitting in Terminal or the Finder, that basic keystroke gets me to the same place.
November 2nd, 2006 at 5:07 pm
You don’t need to use something as verbose as “cd ~/” to change to your home directory. A bare “cd” will do the trick (as would “cd ~”, but “cd” is obviously the shortest one).
November 2nd, 2006 at 5:09 pm
Tom: Fair enough, but I really could care less since it’s in a script that I will never type again.
November 2nd, 2006 at 5:42 pm
Interesting. I’ve been doing sort of the same for a couple years, however with a slightly different twist. Normally I create all windows with the same color, but sometimes I just get overwhelmed by the 30 or so open windows. Also, I don’t like negative contrast type. So I created this script that colorizes *all* open Terminal windows in a light pastel color. As with your solution, if I don’t like it, I just re-apply.
November 2nd, 2006 at 6:58 pm
It’s even shorter in zsh if you have the AUTO_CD option set. In this situation, “~” (as with any other directory) is enough to cd there.
November 2nd, 2006 at 10:10 pm
Very cool. I actually have a PHP shell script that I call from my .bash_profile so that each terminal window gets a new color, depending upon how many are open. Basically, it cycles through colors. I’ve been meaning to document it, and I was halfway through writing a variant that supports iTerm as well (I use Terminal myself), but here’s a link:
http://codesorcery.net/old/misc/termcolor.php.txt
YMMV. I have no idea where the iTerm support stands.
November 2nd, 2006 at 11:01 pm
Nice tip!
One suggestion – I personally like a little transparency in my terminal windows – easily done by adding a fourth parameter to the myBackgroundColor line to set the alpha:
set myBackgroundColor to {randomRed, randomGreen, randomBlue, -9000}
I suppose I could look something up, but empirically, negative numbers seem to be treated “mod kColorValueMaximum”, so -9000 is the same as 56535. Don’t quote me though :-D
I’ve incorporated the Applescript into a shell script kept in ~/bin; it’s now replacing a similar but older script that used Perl and osascript (but was significantly slower and therefore annoying whenever I’d open a new window.)
November 2nd, 2006 at 11:53 pm
It is trivial to add
osascript PathToScript/RandomColorTerminal.scpt
to .bash_profile to get a new color for each terminal session. In addition, you can set an alias such that you can use a short command from the command line to get a new color scheme:
alias newcolor='osascript PathToScript/RandomColorTerminal.scpt'
November 3rd, 2006 at 3:56 am
So who’s writing the background application that keeps the background colour of my Terminal windows in sync with the current light conditions outside?
(I’d expect taking into account time zone or preferably the precise location [which in the crappy Unix world of OS X you may not be able to specify as precisely as with the good old Map control panel] – including current weather data will be an optional extra ;)
November 3rd, 2006 at 4:37 am
I”™d expect taking into account time zone or preferably the precise location
Dude. Ambient light sensor.
November 3rd, 2006 at 9:10 am
So am I the only one this doesn’t work for, then? I run the script, and Terminal just stays with my default white-on-black color scheme. :(
November 3rd, 2006 at 9:14 am
Jon: I suppose it’s worth asking what version of OS X you’re running. And when you run the script from Script Editor, does it fail with any kind of error, or just silently not do anything?
November 3rd, 2006 at 1:29 pm
It doesn’t work for me either. 10.4.8 on a Intel iMac. Script Editor doesn’t give any errors.
November 3rd, 2006 at 2:33 pm
Weird – it works on my PowerPC G5 and my MacBook Pro Core Duo.
November 3rd, 2006 at 3:14 pm
Visor was breaking the script somehow. After I disabled it, the script works.
November 5th, 2006 at 9:20 pm
How about a AppleScript to go to the folder to the top window in finder
tell application “Terminal”
try
tell application “Finder” to set theFolder to target of window 1 as alias
if (count of every window) is 0 then
do script “cd ‘” & POSIX path of theFolder & “‘”
else
if not busy of window 1 then
do script “cd ‘” & POSIX path of theFolder & “‘” in window 1
else
do script “cd ‘” & POSIX path of theFolder & “‘”
end if
end if
end try
end tell
November 5th, 2006 at 9:28 pm
HI Nathan – great minds think alike. I actually have a similar script available on my free AppleScripts page:
http://www.red-sweater.com/AppleScript/
“Terminal at Selection” … I couldn’t live without it. I also added some logic that will go ahead and chose the path of the folder that’s selected, if there is one. So you can for instance type-select a folder and hit the keyboard shortcut to jump there.
November 7th, 2006 at 9:15 am
mick – ahhh, Visor was causing my problems too. I use way too many terminal windows for Visor to be helpful anyway, so no big loss.
June 23rd, 2007 at 7:08 am
Thanks! This is just what I was looking for.
One problem: I don’t like bright background colours. I wanted different dark colours so I could tell terminal windows apart.
So I changed your script to only give me dark background colours. After your global declaration:
— Use a different maximum to keep our background dark
global kDarkColorValueMaximum
set kDarkColorValueMaximum to 20000 — a good max for dark colours
And I changed the set randomRed/Blue/Green to use my constant:
set randomRed to (random number) * kDarkColorValueMaximum
And then I added it to my .profile. Now each terminal window is different.
June 23rd, 2007 at 8:03 am
I just found that simply adding
osascript /PATH/TO/RandomColorTerminal.scpt
to your .profile is a bad idea. If you run any bash scripts, you will find that your terminal colour will suddenly change. Currently, my work-around is to check the current shell level in my .profile before calling the script:
if [ $SHLVL -eq 1 ]; then
osascript /PATH/TO/RandomColorTerminal.scpt
fi
However, there are still problems. If I create two terminals fast enough (Cmd-N, Cmd-N), then I get one regular colour Terminal and one who’s colour changes twice. Does anyone have a fix that only changes the invoking Terminal (pass in some kind of ID, like $PPID or so)?