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


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.
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 | # ]