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 ='')
  terminal.do_script( cmd, :in =>[1] )

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

topmost_window ='').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]
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.