Open a New Terminal Window in the Current Working Directory on a Remote Machine

A short rb-appscript script that opens a new window in Terminal in the current working directory on a remote machine. This is much quicker than launching a new window and having to cd manually when you are deep in your directory hierarchy:


require 'appscript'

def run_command_in_frontmost_window( cmd )
  terminal = Appscript.app('Terminal.app')
  terminal.do_script( cmd, :in => terminal.windows[1] )
end

def open_new_window_with_same_command
  Appscript.app("System Events").keystroke('n', :using => [:command_down, :control_down] )
end    

topmost_window = Appscript.app('Terminal.app').windows[0]
dir_re = /\/(.*\Z)/
title_string = topmost_window.custom_title.get
current_directory = title_string.match( dir_re ).nil? ? nil : title_string.match( dir_re )[1]
open_new_window_with_same_command
run_command_in_frontmost_window( "cd #{current_directory}" ) unless current_directory.nil?

I have this bound to ⌃⌥⌘N using FastScripts to extend the native New Window (⌘N) and New Window with Same Command (⌃⌘N) commands.