Scripting Mac Terminal Using Ruby
Posted by Chris Blackburn Sat, 03 May 2008 01:42:00 GMT
Thanks to Solomon White for his insights. I built the following rough script using this blog article. It fits my own Rails application workflow, so please modify it to your own taste.
I have this script setup to change colors for each type of tab, regular command line, script/console and script/server.
Enjoy…
#!/usr/bin/env ruby
require 'rubygems'
require 'appscript'
include Appscript
DEFAULT_PROJ = "~/Source/ruby/the_mom_trust"
project_dir = (ARGV.length > 0) ? ARGV[0] : DEFAULT_PROJ
project_dir = Dir.pwd if project_dir == "."
# code tab
term = app('Terminal')
first_tab = term.do_script("")
current_window = term.windows.first
tab = current_window.tabs.last
term.do_script("cd #{project_dir} && clear && e", :in => tab)
# script/console tab
app("System Events").application_processes["Terminal.app"].keystroke("t", :using => :command_down)
tab = current_window.tabs.last
term.do_script("cd #{project_dir} && clear && script/console", :in => tab)
tab.normal_text_color.set('white')
tab.background_color.set([0,11992,0]) # dark green
tab.cursor_color.set('yellow')
# script/server tab
app("System Events").application_processes["Terminal.app"].keystroke("t", :using => :command_down)
tab = current_window.tabs.last
term.do_script("cd #{project_dir} && clear && script/server", :in => tab)
tab.normal_text_color.set('white')
tab.background_color.set('blue')
tab.cursor_color.set('red')