Subscribe to
Posts
Comments
NSLog(); Header Image

Terminal Color Changing via AppleScript

I'm not a perpetual Terminal user in that I don't always have a Terminal window open. Instead, I close them between tasks, forcing me to open new windows roughly 1200 times per day. 😛

The Terminal ships with a few default color schemes, like "Black on light yellow," and when I have more than one terminal window open I often change the colors. I use a red terminal with white text to signify that I'm doing something (like downloading a large file) and that the terminal isn't accepting input. I'll apply blue to my own computer's terminal sessions, and I like to change the terminal color to yellow text on a black background when I plan on being root for more than a few seconds.

Other times, I use the colors just to tell one SSH session apart from another.

At any rate, what I'd like to request of the LazyWeb ((I'm not a big fan of the LazyWeb. Which explains why I solved the problem for myself. See the updates.)) is a utility that allows me to change colors among a pre-defined set that I control. Apple's pre-defined styles are great, but I'd like to set up my own and, with the press of a key or two ((Even if I have to use a third-party keyboard shortcut dealio.)), change the color of my terminal window just like I would if I popped open the info panel and adjusted some of the colors.

Can it be done?

Update: Yes, it can be done. Easily. AppleScript is a fairly viable method. Witness:

tell application "Terminal"
    set background color of first window to {15716, 0, 0, -10240}
    set cursor color of first window to "white"
    set normal text color of first window to "white"
    set bold text color of first window to "yellow"
end tell

Witness also the confusing use of colors in the first line. This will set the terminal to a dark red. The first three numbers correspond to RGB values, and the last to opacity. Yet… they're not a straight 0-SOMENUM sort of numbers. You can get negatives, you can get positives, and the range is quite large - values of 30208 and -2334 are common. I've yet to determine the pattern.

For the keyboard shortcuts, and since this is an AppleScript solution, FastScripts handles the keyboard shortcut-izing. You can also adjust things like the number of rows and columns. This solution will be near perfect for me if only I can figure out those zany AppleScript colors.

For now, simply setting the Terminal up as I wish and using the get will have to do.

Update Two: You could also set your terminal to change colors automatically by creating a set of aliases in your shell that run these AppleScript commands, like this:

alias ssserver 'osascript "/Users/iacas/Library/Scripts/Applications/Terminal/Set to Yellow.scpt";ssh user@server.com'

(This sure beats attempting to write the scripts via osascript -e.) So, with that, I have just about all I could really want.

6 Responses to "Terminal Color Changing via AppleScript"

  1. Scripting terminal always breaks it so that .term files can no longer be opened from the Finder. Not sure why, but the bug's been in there for several OSX versions.

  2. I think the numbers for the RGB values are 0-65535 (short ints), but that you're seeing negative numbers sometimes just because AppleScript is interpreting them as signed ints.

    I was inspired by your post to put together a little script that sets the color of the terminal randomly:

    http://www.red-sweater.com/blog/220/random-color-terminal

  3. The RGB values are short ints. I wrote an Applescript a few years ago (still use it) that gives me a list of background color combinations with esthetic text colors and some transaprency so I can chose to color the frontmost terminal window. I then wrote a simple shell script that ran the Applescript with using 'open -a':

    #!/bin/sh
    open -a "/Users/daniello/Library/Scripts/Terminal/ChangeColor"

    My simple color list in the ChangeColor script:

    property MAX_COLOR : 1024 * 10
    property GRAY_MIX : 1024 * 6
    property TRANSPARENCY : -8536
    property colorList : [{MAX_COLOR, 0, 0, TRANSPARENCY}, ¬
    {0, MAX_COLOR, 0, TRANSPARENCY}, ¬
    {0, 0, MAX_COLOR, TRANSPARENCY}, ¬
    {MAX_COLOR, 0, MAX_COLOR, TRANSPARENCY}, ¬
    {MAX_COLOR, GRAY_MIX, 0, TRANSPARENCY}, ¬
    {GRAY_MIX, GRAY_MIX, GRAY_MIX, TRANSPARENCY}]
    property colors : {"Red", "Green", "Blue", "Purple", "Orange", "Gray"}

    I just index from the 'colors' choice.
    I like the Random idea.

  4. [...] 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! [...]

  5. For picking colors, you want to use the "DigitalColor Meter" -- look in /Applications/Utilities

    Set it to "RGB As Actual Value, 16-bit" and then point it at a color on the screen: it'll give you the first 3 numbers to use in your script. The 4th number is indeed saturation -- you can skip it if you want to.

  6. [...] Terminal AppleScript2 MySQL Dock Window Styles Time Machine Spaces Cover Flow/QuickLook Firewall Overall Impressions [...]


Comments RSS

Leave a Reply


Warning: Undefined variable $user_ID in /var/www/vhosts/nslog.com/httpsdocs/wp-content/themes/nslog/comments.php on line 96

Please abide by the comment policy. Valid HTML includes: <blockquote><p>, <em>, <strong>, <ul>, <ol>, and <a href>. Please use the "Quote Me" functionality to quote comments.