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

A script to cycle Terminal color/transparency settings Apps
I wrote a shell script that makes the Terminal not only transparent, but each time you open a new shell, it is a different color. This hint assumes you're running the tcsh shell; it should be relatively simple to modify it for the 10.3 bash default shell. Here's how I did it.

First, open a new Terminal window, and type the following commands:
$ mkdir .colors
$ cd .colors
This creates a hidden directory you'll use to hold your color terminal commands and script, and then switches to that directory. The next step is to create the color files that will tell the Terminal window to switch its background color. Read the rest of the hint for those steps, as well as the changes needed to make this work on each login.

To create the Terminal window color files, copy and paste these commands (skip the $ prompt if you're copying and pasting):
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {47802,6909,1222, -20750}'" > col1.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {2570,33924,20, -20750}" > col2.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {21570,3324,5000, -20750}" > col3.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {30810,26728,12934, -20750}" > col4.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {23387,2313,59367, -20750}" > col5.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {2570,33924,46774, -20750}" > col6.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {59367,23387,2313, -20750}" > col7.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {2313,5367,23387, -20750}" > col8.sh
$ chmod u+x col*.sh
Each of these lines creates a one-line file that executes an AppleScript to change the Terminal window's color (the last line tells the Terminal that the above files are executable, so they can be run as programs). The first three numbers in the curly brakcets are the color values needed to create certain on-screen colors; you can get ideas for different color values from the DigitalColor Meter in /Applications -> Utilities. Set the pop-up to "RGB as Actual Value, 16-bit" to see the values to use. The fourth value in the brackets (the negative one) represents the amount of transparency for the window. It can be adjusted accordingly, but I recommend that you don't put it above -1000 or the effect is basically unnoticeable. -20750 is the value that achieves the amount of transparency you'd see if you adjust the transparency slider to the fourth notch in the GUI. If you want solid windows, just remove this fourth value entirely.

The last step is to modify (or create, if you don't have one) your .login file. Type cd and hit return to jump back to your home directory, and then launch your favorite editor (vi, pico, etc.) on your .login file (e.g. vi .login). Once in the editor, add these lines:
/.colors/col1.sh
mv .colors/col1.sh .colors/tmpcolor.sh
mv .colors/col2.sh .colors/col1.sh
mv .colors/col3.sh .colors/col2.sh
mv .colors/col4.sh .colors/col3.sh
mv .colors/col5.sh .colors/col4.sh
mv .colors/col6.sh .colors/col5.sh
mv .colors/col7.sh .colors/col6.sh
mv .colors/col8.sh .colors/col7.sh
mv .colors/tmpcolor.sh .colors/col8.sh
The first line executes the first color-changing script. The remaining lines then rotate each of the additional scripts up one position in the 'queue,' so that the next time you open a Terminal window, you'll get a new color. Save the file and quit the editor. You can, obviously, use as many or as few color files as you like, just make sure you modify the .login rotation script to match the number of color files you've defined.

For all this trickery to work, open Terminal's Preferences and make sure the following options are set:
  • Execute this command: /bin/tcsh
  • Terminal type ($TERM): ansi
That's it! Restart your Terminal, and watch the colors change as you open new windows.
    •    
  • Currently 2.50 / 5
  You rated: 2 / 5 (6 votes cast)
 
[20,698 views]  

A script to cycle Terminal color/transparency settings | 37 comments | Create New Account
Click here to return to the 'A script to cycle Terminal color/transparency settings' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Possibly Easier?
Authored by: sharumpe on Aug 25, '04 12:07:19PM

How about a short perl (or shell, if that's your preferred poison) script to just use the 'defaults' command to change the background color every time you login (ie. open a terminal window)?

This is non-functional, but something like this:

#!/usr/bin/perl

# Colors to choose from, in R G B
@colors = ( "124 248 182", "219 183 28", "55 27 138" );

# Randomly select one
$nextIndex = int( rand( scalar( @colors ) ) );

# Set the new background color via the 'defaults' command
exec( "defaults", 
      "write",
      "com.apple.terminal",
      "BackgroundColor",
      $colors( $idx ) );

I didn't poke around in the tags for Terminal, but something like that should be possible...

Call this script from your .profile or .login file, and every time you open a terminal, the script will select a random color from the list for the next new window.

Mr. Sharumpe



[ Reply to This | # ]
Possibly Easier?
Authored by: otomo on Aug 26, '04 02:21:02AM
Close, but the value you change is a string of float numbers, I just figured out where the foreground and background were.

Randomness gets you some nasty combinations though.

Anyone know how to start the Terminal from the command line and get a new window and not another instance of Terminal.app?

Then I could just throw this in the dock and run it. As is it is a minor novelty. SAVE YOUR CURRENT SETTINGS!!!

chmod 700 or u+x depending on your permission paranoia.

#!/usr/bin/perl
#
# format appears to be as follows background_rgb[0-2] foreground_rgb[12-14]
# so just grab the old values, plop them into an array
# write over the area for fg and bg and call defaults
@colors = split ' ', `defaults read com.apple.terminal TextColors`;

#cheap, set the fg(text) then the bg colors randomly
(@colors[0], @colors[1], @colors[2]) = (rand(1), rand(1), rand(1));
(@colors[12], @colors[13], @colors[14]) = (rand(1), rand(1), rand(1));

$colorstring = undef;

foreach $x (@colors) {
        $tmp = sprintf "%1.3f", $x;
        $colorstring = $colorstring ? "$colorstring $tmp" : $tmp;
}
system("defaults", "write", "com.apple.terminal", "TextColors", $colorstring);


[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: smorr on Aug 25, '04 12:55:21PM
Very cool -- thanks for the tip

I didn't like all the . files so I took the liberty of condensing it to one osascript call that will do the same. Then I added text color (normal and bold) as 2 additional calls in the .login Here is my .login file

# will cycle through red, green, blue, black, white, fuschia, aqua, orange, and yellow
osascript -e 'tell application "Terminal" to \ 
   set background color of window 0 to item ((number of windows) mod 9 +1) of \
   {{-1, -1, 0, -10000}, {-1, 0, 0, -10000}, {0, -1, 0, -10000},\
   {0, 0, -1, -10000}, {0, 0, 0, -10000}, {-1, -1, -1, -10000},\
   {-1, 0, -1, -10000}, {0, -1, -1, -10000}, {-1, 32767, 0, -10000}}'
osascript -e 'tell application "Terminal" to \ 
   set normal text color of window 0 to item ((number of windows) mod 9 +1 ) of \ 
   {"black","black","black",\
   "white","yellow","blue",\
   "black","black","black"}'
osascript -e 'tell application "Terminal" to \ 
   set bold text color of window 0 to item ((number of windows) mod 9 +1) of \
    {"black","black","black",\
   "white","yellow","blue",\
   "black","black","black"}'

     

[ Reply to This | # ]
less painful pastels
Authored by: SOX on Aug 25, '04 02:18:51PM

try setting those -1,0,0 color values to permuations of 60000,50000,40000 for more eye-pleasing pastels. the defaults make you want to scoop your eyes out with a spoon.



[ Reply to This | # ]
less painful pastels
Authored by: smorr on Aug 25, '04 04:01:08PM

So that explains the seizures -- thanks!



[ Reply to This | # ]
less painful pastels
Authored by: JJ on Aug 26, '04 07:25:50AM

could you give an example? only for -1,0,0.... and how do permutations work?



[ Reply to This | # ]
Rats...
Authored by: demmons65 on Aug 25, '04 12:55:23PM

I wish I had time to dig into this today but I don't.

Anyone else see this message when restarting Terminal?

/.colors/col1.sh: line 1: unexpected EOF while looking for matching `''

---
d a v e

http://www.hostwerks.com/~dave/



[ Reply to This | # ]
Rats...
Authored by: smorr on Aug 25, '04 01:12:02PM
I think the original post is missing ' s and has " in place of ' s

as follows:

$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {47802,6909,1222, -20750}'" > col1.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {2570,33924,20, -20750}" > col2.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {21570,3324,5000, -20750}" > col3.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {30810,26728,12934, -20750}" > col4.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {23387,2313,59367, -20750}" > col5.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {2570,33924,46774, -20750}" > col6.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {59367,23387,2313, -20750}" > col7.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {2313,5367,23387, -20750}" > col8.sh
should be:
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {47802,6909,1222, -20750}' > col1.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {2570,33924,20, -20750}' > col2.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {21570,3324,5000, -20750}' > col3.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {30810,26728,12934, -20750}" > col4.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {23387,2313,59367, -20750}' > col5.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {2570,33924,46774, -20750}' > col6.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {59367,23387,2313, -20750}' > col7.sh
$ echo "osascript -e 'tell front window of app "Terminal" to \
set background color to {2313,5367,23387, -20750}' > col8.sh


[ Reply to This | # ]
Rats...
Authored by: mstoops on Aug 25, '04 01:34:53PM

Actually, it should be:

$ echo "osascript -e 'tell front window of app \"Terminal\" to \
set background color to {47802,6909,1222, -20750}'" > col1.sh
$ echo "osascript -e 'tell front window of app \"Terminal\" to \
set background color to {2570,33924,20, -20750}'" > col2.sh
$ echo "osascript -e 'tell front window of app \"Terminal\" to \
set background color to {21570,3324,5000, -20750}'" > col3.sh
$ echo "osascript -e 'tell front window of app \"Terminal\" to \
set background color to {30810,26728,12934, -20750}'" > col4.sh
$ echo "osascript -e 'tell front window of app \"Terminal\" to \
set background color to {23387,2313,59367, -20750}'" > col5.sh
$ echo "osascript -e 'tell front window of app \"Terminal\" to \
set background color to {2570,33924,46774, -20750}'" > col6.sh
$ echo "osascript -e 'tell front window of app \"Terminal\" to \
set background color to {59367,23387,2313, -20750}'" > col7.sh
$ echo "osascript -e 'tell front window of app \"Terminal\" to \
set background color to {2313,5367,23387, -20750}'" > col8.sh
$ chmod u+x col*.sh



[ Reply to This | # ]
Rats...
Authored by: Batshua on Aug 26, '04 01:31:30AM

tcsh: /.colors/col1.sh: Command not found.

Someone please help, I'm clueless on this one.



[ Reply to This | # ]
Rats...
Authored by: JJ on Aug 26, '04 05:47:09AM
There's an error in the hint:
/.colors/col1.sh
is an absolute reference to rootlevel of the harddisk

it should be
.colors/col1.sh
without the first slash

[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: uurf on Aug 25, '04 01:20:07PM
bash users, add this to your .bashrc:

# will cycle through red, green, blue, black, white, fuschia, aqua, orange, and yellow
osascript -e 'tell application "Terminal" to set background color of window 0 to item ((number of windows) mod 9 +1) of {{-1, -1, 0, -10000}, {-1, 0, 0, -10000}, {0, -1, 0, -10000},{0, 0, -1, -10000}, {0, 0, 0, -10000}, {-1, -1, -1, -10000},{-1, 0, -1, -10000}, {0, -1, -1, -10000}, {-1, 32767, 0, -10000}}'
osascript -e 'tell application "Terminal" to set normal text color of window 0 to item ((number of windows) mod 9 +1 ) of {"black","black","black","white","yellow","blue","black","black","black"}'
osascript -e 'tell application "Terminal" to set bold text color of window 0 to item ((number of windows) mod 9 +1) of {"black","black","black","white","yellow","blue","black","black","black"}'
# don't forget the cursor color
osascript -e 'tell application "Terminal" to set cursor color of window 0 to item ((number of windows) mod 9 +1) of {"black","black","black","white","yellow","blue","black","black","black"}'


(there should be six lines of text - no line feeds - in your .bashrc)

[ Reply to This | # ]

A script to cycle Terminal color/transparency settings
Authored by: uurf on Aug 27, '04 05:00:52AM
hmm, works dandy on the desktop, but get this on my laptop:

## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)


What happened?

[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: kholburn on Dec 22, '04 10:40:30PM

Roxio toast



[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: fitzage on Aug 02, '05 01:54:39PM

I created .bashrc in my home folder, and added this information. It didn't do anything. Is there something else I need to do?



[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: jaysoffian on Aug 25, '04 04:51:29PM
I've been doing this for ages. I may as well add my solution to the mix. Got it from a co-worker who was doing with Python and eTerm under Linux and re-wrote to use Applescript. Here's the Applescript:

property maxr : 0.35
property minr : 0.15
property quad : 70
-- property transparency: -8000
property transparency : 65535

on sin(x)
	repeat until x ? 0 and x < 360
		if x ? 360 then
			set x to x - 360
		end if
		if x < 0 then
			set x to x + 360
		end if
	end repeat
	
	--convert from degrees to radians
	set x to x * (2 * pi) / 360
	
	set answer to 0
	set numerator to x
	set denominator to 1
	set factor to -(x ^ 2)
	
	repeat with i from 3 to 40 by 2
		set answer to answer + numerator / denominator
		set numerator to numerator * factor
		set denominator to denominator * i * (i - 1)
	end repeat
	
	return answer
end sin

on cos(x)
	repeat until x ? 0 and x < 360
		if x ? 360 then
			set x to x - 360
		end if
		if x < 0 then
			set x to x + 360
		end if
	end repeat
	
	--convert from degrees to radians
	set x to x * (2 * pi) / 360
	
	set answer to 0
	set numerator to 1
	set denominator to 1
	set factor to -(x ^ 2)
	
	repeat with i from 2 to 40 by 2
		set answer to answer + numerator / denominator
		set numerator to numerator * factor
		set denominator to denominator * i * (i - 1)
	end repeat
	
	return answer
end cos

on spherical2rect(r, phi, theta)
	phi = (pi / 180) * phi
	theta = (pi / 180) * theta
	return {r * (sin(phi)) * (cos(theta)), r * (sin(phi)) * (sin(theta)), r * (cos(phi))}
end spherical2rect

on randcolor()
	set r to (random number (maxr - minr)) + minr
	set phi to (random number quad) + (90 - quad) / 2 + 10
	set theta to (random number quad) + (90 - quad) / 2
	set xyz to spherical2rect(r, phi, theta)
	set rgb to {
		round (65535 * (item 1 of xyz)) rounding down, 
		round (65535 * (item 2 of xyz)) rounding down, 
		round (65535 * (item 3 of xyz)) rounding down}
end randcolor

tell application "Terminal"
	tell window 1 to set background color to ((my randcolor()) & transparency)
end tell
I save that as ~/Library/Scripts/Random Terminal Color. In my ~/bin directory is a script called rc:

#!/bin/sh
exec osascript "$HOME/Library/Scripts/Random Terminal Color"
In my .login (yes, I still use tcsh, it's embarrasing) is:

if ($?TERM_PROGRAM) then
        if ($TERM_PROGRAM == "Apple_Terminal") then
                $HOME/bin/rc
                setenv TERM xterm-color
        endif
endif
If I don't like the random color that gets picked I can either type "rc" at the shell prompt or just choose "Random Terminal Color" from the menu scriptlet. I got tired of transparent windows after a while. If you like them, play around with the tranparency property set at the top of the script. Here's a python version of the script if you prefer:

#!/usr/bin/python

import os
import sys
import random
import math

maxr = .35
minr = .15
quad =  70

def spherical2rect(r,phi,theta):
	phi   = (math.pi/180)*phi
	theta = (math.pi/180)*theta
	return (r*math.sin(phi)*math.cos(theta),r*math.sin(phi)*math.sin(theta),r*math.cos(phi))

# Generate a random location in the +++ spherical octant

r     = random.random()*(maxr-minr) + minr
phi   = random.random()*quad + (90-quad)/2 + 10
theta = random.random()*quad + (90-quad)/2

(x,y,z) = spherical2rect(r,phi,theta)


v1 = int(65535*x)
v2 = int(65535*y)
v3 = int(65535*z)
transparency=-5000

s = """
tell application "terminal"
	tell window 1 to set background color to {%d, %d, %d, %d}
end tell
""" % (v1,v2,v3,transparency)

p = os.popen("/usr/bin/osascript","w")
p.write(s)
p.close()
The Applescript version runs slightly faster since the Python version ends up invoking osascript anyway.

[ Reply to This | # ]
A slightly nicer & prettier script...
Authored by: dbs on Aug 26, '04 06:41:39PM
Here is a slightly nicer AppleScript to do this. It allows you to have either white-on-color or color-on-white windows, or randomly select them. It sets the value and saturation appropriately for either setting and then randomly selects the hue. This dramatically reduces the number of ugly colors you get. To force it to always choose white on black or vice versa simply change the
random_select_white_on_black
property to false and set the other to whatever you want. To run it do the same as above by calling the apple script from a command line. I use a script called color which has:

#!/bin/sh
exec osascript "$HOME/Library/Scripts/Better Random Terminal Color.scpt"
Then I call it from my .cshrc if I am running Apple's Terminal Program by putting this in the file:

# Set a random terminal color
if ($?TERM_PROGRAM) then
    if ($TERM_PROGRAM == "Apple_Terminal") then
        $HOME/bin/color
        setenv TERM xterm-color
    endif
endif
The Apple Script is:

property transparency : 65535
property dark_saturation : 1.0
property dark_value : 0.3
property light_saturation : 0.1
property light_value : 0.9
-- Enable to randomly select if you want white on black or black on white text
property random_select_white_on_black : true
-- If it is not random this controls which color scheme you get
property white_on_black : true

-- Based on http://www.cs.rit.edu/~ncs/color/t_convert.html
-- Note that there is an error on that page with regards to f
on HSVtoRGB(h, s, v)
	--log "HSBtoRGB: h:" & h & " v:" & v & " s:" & s
	-- h in degrees
	-- s in 0 to 1.0
	-- v in 0 to 1.0
	
	if s = 0 then
		-- Gray, so set it accordingly
		set r to v
		set g to v
		set b to v
	else
		set sector to h / 60 -- sector for the hue
		set i to round (sector) rounding down
		set f to (h - i) / 360 -- remaining bit of h
		set p to v * (1 - s)
		set q to v * (1 - s * f)
		set t to v * (1 - s * (1 - f))
		
		if i = 0 then
			set r to v
			set g to t
			set b to p
		else if i = 1 then
			set r to q
			set g to v
			set b to p
		else if i = 2 then
			set r to p
			set g to v
			set b to t
		else if i = 3 then
			set r to p
			set g to q
			set b to v
		else if i = 4 then
			set r to t
			set g to p
			set b to v
		else
			set r to v
			set g to p
			set b to q
		end if
		
	end if
	
	--log "HSV2RGB - R: " & r & " G: " & g & " B: " & b
	
	set rgb to {r * 65535, g * 65535, b * 65535}
end HSVtoRGB


tell application "Terminal"
	set h to (random number 360)
	
	-- If we want to randomly choose between white and black text pick one
	if random_select_white_on_black is true then
		if (random number 1) > 0.5 then
			set white_on_black to true
		else
			set white_on_black to false
		end if
	end if
	-- Otherwise use the default above
	
	if white_on_black is false then
		-- Set to random color and black text..	
		tell window 1 to set background color to ((my HSVtoRGB(h, light_saturation, light_value)) & transparency)
		tell window 1 to set normal text color to ({0, 0, 0})
	else
		-- set to random color and white text/bold
		tell window 1 to set background color to ((my HSVtoRGB(h, dark_saturation, dark_value)) & transparency)
		tell window 1 to set normal text color to ({55000, 55000, 55000})
		tell window 1 to set bold text color to ({65535, 65535, 65535})
	end if
end tell
Enjoy! (And thanks to the others for the ideas!)

[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: Batshua on Aug 26, '04 12:18:54AM

Can anyone tell me if changing the filenames consistently will break this?

I wanted to name the files with descriptors so I can tell what colors they are in case I want to delete or change one later.



[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: JJ on Aug 26, '04 05:55:19AM

The problem with that would appear to be that the original script cycles through different colors by renaming the scripts in a loop; 1->8, 2->1, 3->2, ... , 8->7

So a filename with any significance would quickly lose the its meaning

You could put a comment inside the files themselves



[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: thinkyhead on Aug 26, '04 09:11:36PM
Make it shorter than mine and you win!

#!/usr/bin/perl
$min = ($#ARGV==0 && $ARGV[0]=~/bright/i) ? 45500 : 0;
$r = int(rand(20000)) + $min;
$g = int(rand(20000)) + $min;
$b = int(rand(20000)) + $min;
$col = "$r,$g,$b, -9000";
`osascript -e 'tell front window of app "Terminal" to set background color to {$col}'`;

---
|
| slur was here
|

[ Reply to This | # ]

ZEORGE MADE IT BEST :) HEHE
Authored by: zeorge on Aug 27, '04 02:18:42PM
use this script from your .tcshrc or .bashrc it does not use apple script so its 1000 times faster. you can setup a color list - random colors are chosen. save it als ~/.setcolor.pl for example, and dont forget to chmod +x ~/.setcolor.pl. #!/usr/bin/perl # zeorge 08-2004 - terminal BG color changer - works with panther 10.3.5 # Colors to choose from, in R G B @color_table = ( "0.200 0.000 0.000","0.000 0.200 0.000","0.000 0.000 0.200" ); $opaqueness = "0.95"; #defaults read com.apple.terminal TextColors @text_colors = split " ",`defaults read com.apple.terminal TextColors`; # Randomly select a Color $index = int( rand( scalar( @color_table ) ) ); @colors = split " ", @color_table[$index]; # set selected color in TextColors array # pos 1 and 4 of TextColors = background color $pos = 1 ; @text_colors[$pos*3] = @colors[0]; @text_colors[$pos*3+1] = @colors[1]; @text_colors[$pos*3+2] = @colors[2]; $pos = 4 ; @text_colors[$pos*3] = @colors[0]; @text_colors[$pos*3+1] = @colors[1]; @text_colors[$pos*3+2] = @colors[2]; # ( pos 0 and 4 of TextColors = text color # pos 2 and 3 of TextColors = text bold color # pos 6 of TextColors = selection color # pos 7 of TextColors = cursor color ) # Set the new background color via the 'defaults' command system("defaults","write","com.apple.terminal","TextColors","@text_colors"); system("defaults","write","com.apple.terminal","TerminalOpaqueness","$opaqueness");

[ Reply to This | # ]
ZEORGE MADE IT BEST :) HEHE
Authored by: zeorge on Aug 27, '04 02:27:10PM

use this script from your .tcshrc or .bashrc
it does not use apple script so its 1000 times faster.
you can setup a color list - random colors are chosen.

save it als ~/.setcolor.pl for example,
and dont forget to chmod +x ~/.setcolor.pl.

<code>
#!/usr/bin/perl
# zeorge 08-2004 - terminal BG color changer - works with panther 10.3.5

# Colors to choose from, in R G B
@color_table = ( "0.200 0.000 0.000","0.000 0.200 0.000","0.000 0.000 0.200" );
$opaqueness = "0.95";

#defaults read com.apple.terminal TextColors
@text_colors = split " ",`defaults read com.apple.terminal TextColors`;

# Randomly select a Color
$index = int( rand( scalar( @color_table ) ) );
@colors = split " ", @color_table[$index];

# set selected color in TextColors array
# pos 1 and 4 of TextColors = background color

$pos = 1 ;
@text_colors[$pos*3] = @colors[0];
@text_colors[$pos*3+1] = @colors[1];
@text_colors[$pos*3+2] = @colors[2];
$pos = 4 ;
@text_colors[$pos*3] = @colors[0];
@text_colors[$pos*3+1] = @colors[1];
@text_colors[$pos*3+2] = @colors[2];

# ( pos 0 and 4 of TextColors = text color
# pos 2 and 3 of TextColors = text bold color
# pos 6 of TextColors = selection color
# pos 7 of TextColors = cursor color )

# Set the new background color via the 'defaults' command
system("defaults","write","com.apple.terminal","TextColors","@text_colors");
system("defaults","write","com.apple.terminal","TerminalOpaqueness","$opaqueness");
</code>



[ Reply to This | # ]
zeorge finally :) a super script
Authored by: zeorge on Aug 27, '04 02:44:24PM

which i should not have coded because i got lotta work to do :)

save it as "setcolor.pl", do a "chmod +x setcolor.pl",
and call it as first line from your .tcshrc or bashrc.

it changes the color randomly selecting from a list.
it does not take the same color two times.
it does not use apple script
it is fast
its ugly coded (i know) but better than &*ç% applescript
it is really fast :)

[code]
#!/usr/bin/perl
# zeorge 08-2004 - terminal BG color changer - works with panther 10.3.5

# Colors to choose from, in R G B
@color_table = ( "0.200 0.000 0.000","0.000 0.200 0.000","0.000 0.000 0.200" );
$opaqueness = "0.95";

#defaults read com.apple.terminal TextColors
@text_colors = split " ",`defaults read com.apple.terminal TextColors`;

# get actual bg color
$pos = 1 ;
@bg_color[0] = @text_colors[$pos*3];
@bg_color[1] = @text_colors[$pos*3+1];
@bg_color[2] = @text_colors[$pos*3+2];

# Randomly select a Color BUT NOT THE SAME as the actual bg color
for(;;) {
$index = int(rand(scalar(@color_table)));
@colors = split " ", @color_table[$index];
last if("@colors" != "@bg_color");
}

# set selected color in TextColors array
# pos 1 and 4 of TextColors = background color

$pos = 1 ;
@text_colors[$pos*3] = @colors[0];
@text_colors[$pos*3+1] = @colors[1];
@text_colors[$pos*3+2] = @colors[2];
$pos = 4 ;
@text_colors[$pos*3] = @colors[0];
@text_colors[$pos*3+1] = @colors[1];
@text_colors[$pos*3+2] = @colors[2];

# ( pos 0 and 4 of TextColors = text color
# pos 2 and 3 of TextColors = text bold color
# pos 6 of TextColors = selection color
# pos 7 of TextColors = cursor color )

# Set the new background color via the 'defaults' command
system("defaults","write","com.apple.terminal","TextColors","@text_colors");
system("defaults","write","com.apple.terminal","TerminalOpaqueness","$opaqueness");
[/code]



[ Reply to This | # ]
zeorge finally :) a super script
Authored by: uurf on Aug 27, '04 04:28:00PM

it doesn't like something here:


-bash: /Users/shortname/.bashrc: line 20: syntax error near unexpected token `('
-bash: /Users/shortname/.bashrc: line 20: `@color_table = ( "0.200 0.000 0.000","0.000 0.200 0.000","0.000 0.000 0.200" );'



[ Reply to This | # ]
you cant just copy paste this into your bashrc
Authored by: zeorge on Aug 29, '04 06:57:41AM

you cant just copy paste this into your bashrc...

this is a PERL script. your bashrc is a SHELL script.
(see the first line #!/usr/bin/perl - which is important!)

you must save the script as a file, do a "chmod +x file",
and then you can call it with "file" from your bashrc
zeo



[ Reply to This | # ]
zeorge finally :) a super script
Authored by: mysty on Sep 10, '04 03:39:05PM

Thanks for all these tips

In trying to get this going (10.3 with bash) I made a .bashrc with

. ~/scripts/termcolours.pl

as the first line and did the chmod+x on the above .pl file. Now when I launch a new Terminal window I get

-bash: /Users/me/scripts/termcolours.pl: line 5: syntax error near unexpected token `('
-bash: /Users/mye/scripts/termcolours.pl: line 5: ` @color_table = ( "0.200 0.000 0.000","0.000 0.200 0.000","0.000 0.000 0.200" );'

I think I got perl581 going on (according to Fink at least), so I cant get why Im getting this error.
Any ideas are most welcome. The wierd thing is that I am getting a similar error on line 5 of tim1724's script below as well...

As well as this wierd new .bashrc.swp file, and this console msg:

2004-09-10 19:19:01.779 Safari[2097] (goo, "bashrc.swp")

wtf?! goo?!? *boggle*

I havent relaunched Terminal yet as I was fink updating nmap and it wanted me to reinstall xfree86-base again (......)

TIA for your thoughts



[ Reply to This | # ]
zeorge finally :) a super script
Authored by: raider on Oct 29, '04 10:00:16AM
I combined Slur's and Zorge's scripts. I wanted the randomness of Slur's, but the speed of Zeorge's. Here is what I came up with:

#!/usr/bin/perl
$r = int(rand(2000));
$g = int(rand(2000));
$b = int(rand(2000));
$opaqueness = "0.98";
@text_colors = split " ",`defaults read com.apple.terminal TextColors`;
$pos = 1 ;
@text_colors[$pos*3] = ".$r";
@text_colors[$pos*3+1] = ".$g";
@text_colors[$pos*3+2] = ".$b";
$pos = 4 ;
@text_colors[$pos*3] = ".$r";
@text_colors[$pos*3+1] = ".$g";
@text_colors[$pos*3+2] = ".$b";
system("defaults","write","com.apple.terminal","TextColors","@text_colors");
system("defaults","write","com.apple.terminal","TerminalOpaqueness","$opaqueness");


Works pretty good, but every now and then I get a pretty bac random color... ;) Anyone who can improve the random generation ranges feel free to chime in....

[ Reply to This | # ]
my script
Authored by: tim1724 on Aug 27, '04 06:34:45PM

I use a Perl script which chooses colors based on the HSV system. I have it set to choose pastel colors, but you can configure it easily if you have other preferences.

  • Hue is the color (red,green,orange,purple,etc. ranges from 0 to 360)
  • Saturation is how strong the color is (0=grey, 1=most saturated)
  • Value is the brightness (0=black, 1=full brightness)
  • Alpha is the transparency (0=completely transparent, 1=completely opaque)

#!/usr/bin/perl


# parameters which determine the range of random colors chosen
my  ($min_h,$max_h) =  ( 0,    360   );   # hue        (0-360)
my  ($min_s,$max_s) =  ( 0.30, 0.60  );   # saturation (0-1)
my  ($min_v,$max_v) =  ( 0.75, 1.00  );   # brightness (0-1)
my  ($min_a,$max_a) =  ( 0.65, 0.85  );   # alpha      (0-1)


# generate random number between two values
sub randbetween {
    my ($min,$max) = @_;

    return $min + rand($max-$min);
}



# HSV to RGB conversion algorithm from  http://www.cs.rit.edu/~ncs/color/t_convert.html

sub hsv2rgb {
    my ($h,$s,$v) = @_;

    # handle greyscale case
    return ($v,$v,$v) if ($s == 0);

    my ($i, $f, $p, $q, $t);

    $h /= 60;  # convert to sector between 0 and 5
    $i = int($h);
    $f = $h - $i;
    $p = $v * (1-$s);
    $q = $v * (1-$s*$f);
    $t = $v * (1-$s*(1-$f));

    return ($v, $t, $p) if $i == 0;
    return ($q, $v, $p) if $i == 1;
    return ($p, $v, $t) if $i == 2;
    return ($p, $q, $v) if $i == 3;
    return ($t, $p, $v) if $i == 4;
    return ($v, $p, $q);
}



# turn list of floats into a string suitable for applescript
sub rgb2string {
    return join ", ", map(int(65536*$_),@_);
}




#main program

my @rgb = hsv2rgb(randbetween($min_h,$max_h), randbetween($min_s,$max_s), randbetween($min_v,$max_v));
my $alpha = int(65536*randbetween($min_a, $max_a));
my $color = '{ ' . rgb2string(@rgb). ", $alpha }";

system "osascript", "-e", "tell application \"Terminal\" to set the background color of window 1 to $color";

You could easily have this randomize the text color or cursor color as well, but I prefer to leave them set to black.

---
Tim Buchheim

[ Reply to This | # ]

my script
Authored by: mysty on Sep 10, '04 03:48:13PM
Again (as described in reply to zeorge's post above, 10.3 & bash) I get the following error for a new Terminal window:

-bash: /Users/me/scripts/termcolours2.pl: line 5: syntax error near unexpected token `$min_h,$max_h'
-bash: /Users/me/scripts/termcolours2.pl: line 5: `my  ($min_h,$max_h) =  ( 0,    360   );   # hue        (0-360)'
is it my perl all skew-wiffied?


Characteristics of this binary (from libperl): 
  Compile-time options: MULTIPLICITY USE_ITHREADS USE_LARGE_FILES PERL_IMPLICIT_CONTEXT
  Locally applied patches:
        RC3
  Built under darwin
  Compiled at Sep 12 2003 19:50:49
  %ENV:
    PERL5LIB="/sw/lib/perl5"
  @INC:
    /sw/lib/perl5
    /System/Library/Perl/5.8.1/darwin-thread-multi-2level
    /System/Library/Perl/5.8.1
    /Library/Perl/5.8.1/darwin-thread-multi-2level
    /Library/Perl/5.8.1
    /Library/Perl
    /Network/Library/Perl/5.8.1/darwin-thread-multi-2level
    /Network/Library/Perl/5.8.1
    /Network/Library/Perl
TIA and for the script

[ Reply to This | # ]
my script
Authored by: tim1724 on Sep 14, '04 03:11:51PM

My guess is that copying & pasting the script messed it up somehow.

Try downloading it from my website.

---
Tim Buchheim

[ Reply to This | # ]

my script
Authored by: sweyhrich on Oct 14, '04 06:40:27PM

What exactly is the command to put in .bashrc in order to execute the perl script? I've tried various things, and each time I open a new terminal window, I still get the same colors as it was before.

---
Steven Weyhrich
http://apple2history.org



[ Reply to This | # ]
my script
Authored by: mysty on Sep 10, '04 03:59:16PM
maybe the fact that the

. ~/scripts/termcolours2.pl
line only gets called from the .profile (and not from the .bashrc) is a clue? You can see I am way in the dark

[ Reply to This | # ]
Note for Bash users!
Authored by: raider on Sep 09, '04 06:36:15PM
After struggling to get this to work in the default 10.3 setup (bash) - I found that Terminal is not using .bashrc

If your .bashrc appears to be ignored - try putting this in your .bash_profile file:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

Your mileage may vary. Use at your own risk.

[ Reply to This | # ]
Note for Bash users!
Authored by: blgrace on Dec 22, '04 07:06:43PM
I couldn't get any perl scripts to execute from my .bashrc until I added this line: perl rc.pl instead of the ? standard . ~/rc.pl Does this mean that perl is not in my path?

[ Reply to This | # ]
Note for Bash users!
Authored by: blgrace on Dec 28, '04 02:02:41AM

Hmmm . . . the answer for me turned out to be this.
put the full path to the perl script in the .bashrc file

~/bin/rc.pl

and it works fine



[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: raider on Jun 23, '05 12:28:58AM
Well, I figured I would re-visit this and my script (posted above) for Tiger (10.4.1).

I have been using Tiger for a few months now, and I realized that I missed my random terminal colors. So I implemented the script like I had posted above.

GAWD! In Tiger the colors seem WAY more obnoxious. It could just be me, but Tiger + my script was not cutting it. Everything seemed to assault the eyes!

So I set out to revamp my script. I came up with this:
#!/usr/bin/perl
$r = int(rand(999));
$g = int(rand(999));
$b = int(rand(999));
$opaqueness = "0.98";
@text_colors = split " ",`defaults read com.apple.terminal TextColors`;
$pos = 1 ;
@text_colors[$pos*3] = ".1$r";
@text_colors[$pos*3+1] = ".1$g";
@text_colors[$pos*3+2] = ".1$b";
$pos = 4 ;
@text_colors[$pos*3] = ".1$r";
@text_colors[$pos*3+1] = ".1$g";
@text_colors[$pos*3+2] = ".1$b";
system("defaults","write","com.apple.terminal","TextColors","@text_colors");
system("defaults","write","com.apple.terminal","TerminalOpaqueness","$opaqueness"); 
That gives me much better, more subdued and darker colors. I have my text set to white, but any light color could do...

The important value is the number in these lines:

@text_colors[$pos*3+2] = ".1$b";

If you change those one's that follow the period, you will change the results llightness and darkness. For example, here is a screenshot of ones and here is a screenshot of zeroes. Feel free to experiment...

Remember, you need to save this script as a perl script somewhere, make it executable, and then call it from your .bashrc file... The other comments above describe those steps if you need help....

[ Reply to This | # ]
A script to cycle Terminal color/transparency settings
Authored by: lucianf on Oct 31, '07 11:32:23PM

Anyone knows how to programatically make Terminal windows transparent in Leopard? Colours don't take four parameters any longer, it's just the RGB, and transparency only seems to be available through the settings screen (no mentioning of that in the applescript dictionary either).

If anyone can help, I'd greatly appreciate that.



[ Reply to This | # ]