Posted by Chris Blackburn Fri, 29 May 2009 20:49:00 GMT

Another ruby script for your Mac Terminal enjoyment.

I like to work on my Rails projects using at least three command line tabs with their current working directories set to the RAILS_ROOT of my project. This way I can run script/server in one tab, script/console in another and retain one tab to use for various project based work like rake and capistrano tasks. Frequently I found myself manually iterating through each Terminal tab and type ‘cd /Users/…/Source/ruby/projectname’. As we all know personal repetition is the mother of invention. This is what I came up with…

#!/usr/bin/env ruby

require 'rubygems'
require 'appscript'
include Appscript

DEFAULT_PROJ = '.'

project_dir = (ARGV.length > 0) ? ARGV[0] : DEFAULT_PROJ
project_dir = Dir.pwd if project_dir == "."

@term = app('Terminal')
@current_window = @term.windows.first
tab_count = @current_window.tabs.count

for t in 0..tab_count
  tab = @current_window.tabs[t]
  @term.do_script("cd #{project_dir}", :in => tab)
end

Save this file in your personal path as acd.rb

I use this frequently when I switch from one project to another.

Be logged into the directory you want to propagate through all open tabs. Then type: acd.rb which will automatically iterate through each open tab and change the current working directory to the directory you started acd.rb from.

Posted by Chris Blackburn 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