Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Dynamically Generating Mac OSX Terminal.app Colors

by techy (Scribe)
on Aug 28, 2004 at 17:15 UTC ( #386621=sourcecode: print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info Nathan Byrd (nathan@byrd.net)
Description: I recently got a new iBook to play around with. My only disappointment was that the default Terminal.app uses the standard boring black text on white background. Inspired by the perlmonks node "to generate a set of well contrasted colors" as well as this article on macosxhints, I wrote a script to dynamically change the colors of Terminal.app on each login.

Supported options include:

  • --alpha - set alpha (transparency) level between 0 (transparent) and 100 (opaque)
  • --foreground - Swap foreground and background colors for a subtler effect
  • --(no)transparency - Turn on (default) or off transparency
  • --help - Get help

This code cheats by using "osascript" and applescript to tell Terminal.app to change colors. To run this script on login, add a call to it from your .profile (if using the default bash shell). This script should also be easily modifyable to work with other terminals/xterms/etc.

#!/usr/bin/perl

use Getopt::Long;
use strict;
use warnings;

my $foreground   = 0;
my $transparency = 1;
my $alpha        = 80;
my $help         = 0;
my $result = Getopt::Long::GetOptions(
    'fg|foreground' => \$foreground,
    'transparency!' => \$transparency,
    'alpha:i'       => \$alpha,
    'help|?'        => \$help,
);

die "Usage: $0 [--alpha <number>] [--foreground] [--[no]transparency] 
+[--[no]dark]\n"
    if @ARGV or not $result or $help;
die "Option 'alpha' must be between 0 (transparent) and 100 (opaque)\n
+"
    if $alpha < 0 or $alpha > 100;

$alpha = 100 unless $transparency;
$alpha = ( $alpha * 65535 ) / 100;

my ($bg, $bg1, $fg) = random_colors( );
($fg, $bg) = ($bg, $fg) if $foreground;

$" = ", ";
open(OSA,"|-", "osascript");
print OSA <<SCRIPT;
tell front window of app "Terminal"
set background color to {@$bg, $alpha}
set normal text color to {@$fg}
set bold text color to {@$bg1}
set cursor color to {@$bg1}
end tell
SCRIPT

close(OSA);
exit 0;

sub random_colors {
    my @rgb = map { int rand 256 } 1 .. 3;
    my $lum = ($rgb[0] * 0.3) + ($rgb[1] * 0.59) + ($rgb[2] * 0.11);
    my @rgb_bold = map { ( ( $_ + 128 ) % 256 ) * 257 } @rgb;
    @rgb = map { $_ * 257 } @rgb;
    my @rgb_fg = $lum < 128 ? (65535, 65535, 65535) : (0, 0, 0);
    return (\@rgb, \@rgb_bold, \@rgb_fg);
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://386621]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-01-25 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?