Random Color Terminal

November 2nd, 2006

Erik 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.

19 Responses to “Random Color Terminal”

  1. Tom Foolery Says:

    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).

  2. Daniel Jalkut Says:

    Tom: Fair enough, but I really could care less since it’s in a script that I will never type again.

  3. maurits Says:

    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.

  4. Peter Hosey Says:

    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.

  5. Justin Miller Says:

    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.

  6. Mark Glossop Says:

    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.)

  7. Tyler Kieft Says:

    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'

  8. ssp Says:

    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 ;)

  9. Scott Stevenson Says:

    Iā€™d expect taking into account time zone or preferably the precise location

    Dude. Ambient light sensor.

  10. Jon Says:

    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. :(

  11. Daniel Jalkut Says:

    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?

  12. mick Says:

    It doesn’t work for me either. 10.4.8 on a Intel iMac. Script Editor doesn’t give any errors.

  13. Daniel Jalkut Says:

    Weird – it works on my PowerPC G5 and my MacBook Pro Core Duo.

  14. mick Says:

    Visor was breaking the script somehow. After I disabled it, the script works.

  15. Nathan Says:

    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

  16. Daniel Jalkut Says:

    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.

  17. Jon Says:

    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.

  18. David Says:

    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.

  19. David Says:

    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)?

Comments are closed.

Follow the Conversation

Stay up-to-date by subscribing to the Comments RSS Feed for this entry.