Opening new Terminal window from Finder

Apr 20, '11 07:30:00AM

Contributed by: Anonymous

I use the Terminal and Midnight Commander quite often. However, opening a new terminal window and then changing to the directory location where I need to use the command line takes time. The Toolbar Dragpoints hint brought me to an idea to create a short AppleScript that opens a new terminal window in the folder active in Finder:

on ApplicationIsRunning(appName)
  tell application "System Events" to set appNameIsRunning to exists (processes where name is appName)
  return appNameIsRunning
end ApplicationIsRunning

tell application "Finder"
  try
    set winLoc to target of the front window as text
  on error
    set winLoc to home as text
  end try
  set pth to quoted form of POSIX path of winLoc
end tell

if ApplicationIsRunning("Terminal") then
  tell application "Terminal"
    do script "cd " & pth & ""
  end tell
else
  tell application "Terminal"
    activate
    set twID to index of front window
  end tell
  tell application "Terminal"
    do script "cd " & pth & "" in window twID
  end tell
end if

tell application "Terminal" to activate
You can save the script in the AppleScript Editor as an application and then just drag and drop it into the Finder toolbar. If you have Midnight Commander installed, you can change the first "do script" line to:

do script "mc \"" & pth & "\" \"" & pth & "\""

and the second to:

do script "mc \"" & pth & "\" \"" & pth & "\"" in window twID

In that case the script opens the Midnight Commander with the folder active in both panels. I found this especially useful when I need to extract just one or few files from a zip archive as Midnight Commander allows you to do that.

[crarko adds: I think we've had simliar hints before; I run this one because the Midnight Commander part may be a new wrinkle on it. There was a time when I used to use MC quite a bit, too. I compiled the script to make sure all the quotes matched, let me know if there are any errors with the commands. Script corrected per author's request -- CRA]

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20110414032212445