Posted by admin Wed, 13 May 2009 18:55:00 GMT

Recently I upgraded my blog here and lost permalinks to several articles, as well as the articles themselves. For that I apologize, if you are looking for something unfound. One such article that I referred to often was ‘Scripting Mac Terminal Using Ruby’. Though this is not the original article, here is a script I recently needed. As will all source code published herein, this is hereby released into the public domain with no warranties of any kind.

This humble little script uses rb-appscript, to change the background color, in Terminal, of the current tab you are running. I like to change colors of tabs to mean different things. For instance, where I am tailing log files – dark green… running irb – dark blue, etc.

Maybe, like me, you stay logged into several machines around the world and want to color-code your tabs based on location.

I called mine colorme.rb. The command line expects a color in this format:

  • array: [0,32767,65535] of color values [Red, Green, Blue], 0 to 65535
  • string: ‘red’, ‘green’, ‘black’, etc., only the basic colors work as strings

Have fun and let me know if you are doing something interesting with Terminal using Appscript on Ruby.

#!/usr/bin/env ruby

require 'rubygems'
require 'appscript'
include Appscript

_color = (ARGV.length > 0) ? ARGV[0] : 'black'

begin
  term = app('Terminal')
  current_window = term.windows.first
  tab = current_window.tabs.first
  current_color = tab.background_color.get
  puts "Current Color is: #{current_color.inspect}"
  tab.background_color.set(_color)
rescue Exception => e
  puts "#{e}"
  puts "Usage: colorme.rb array"
  puts "  ...where array is an array of 3 color values like this [0,32767,65535]"
  puts "  ...values can be anywhere between 0 and 65535"
  puts "  ...values, in order, represent 'Red, Green Blue'"
end

Posted by admin Tue, 22 May 2007 23:46:00 GMT

Have you ever wanted to look at the call stack without raising an exception to do it?

caller.each {|c| puts c}