Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Opening new Terminal window from Finder Apps
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]
    •    
  • Currently 4.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[5,371 views]  

Opening new Terminal window from Finder | 13 comments | Create New Account
Click here to return to the 'Opening new Terminal window from Finder' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Opening new Terminal window from Finder
Authored by: karog on Apr 20, '11 07:49:48AM

OpenTerminal.app does the same thing. Google it.



[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: leninwtigger on Apr 20, '11 07:57:15AM

The way I go about this is the following:

I installed Dterm (http://decimus.net/DTerm/).
In whichever folder I am I press the keyboard shortcut that brings up DTerm, type the command and press the keyboard combination for "execute in terminal"

It works well, Dterm is free. Try it.



[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: gloubibou on Apr 20, '11 08:03:33AM
For this hint to work on a path which contains spaces, you need to use the quoted form of the POSIX path:

	set pth to quoted form of POSIX path of winLoc


[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: crarko on Apr 21, '11 08:38:41AM

I edited the script to reflect this, with the author's request.

Thanks!

Craig A.



[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: jaguarz on Apr 20, '11 08:11:22AM

you can also simply drag and drop a file/folder on the terminal window to enter that items path



[ Reply to This | # ]
Problem with spaces...
Authored by: wallybear on Apr 20, '11 08:39:26AM
Nice script, but it's better to replace the line
set pth to POSIX path of winLoc
with
set pth to quoted form of (POSIX path of winLoc)
or the script will not work with folder names containing spaces or unusual characters.


[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: ambrose on Apr 20, '11 08:51:59AM

To add 'New Terminal at Folder' and/or 'New Terminal Tab at Folder' functions to the Finder's contextual menu, navigate via the Finder's menu bar to 'Services --> Services Preferences', and thence via the resulting System Preferences pane to 'Services --> Files and Folders.'



[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: wlbentley on Apr 20, '11 09:19:26AM

Or just add the cdto button to your toolbar...

http://code.google.com/p/cdto/

I find it a bit faster and simpler than OpenTerminal...

Edited on Apr 20, '11 09:36:46AM by wlbentley



[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: odaiwai on Apr 20, '11 10:05:08PM

There's another way to go to the current finder directory in Terminal.app:

1. Open Terminal.app;
2. Type cd ' (that's cd and a space);
3. Go back to the Finder, and option-click on the folder icon in the title bar;
4. Continue holding option and drag the icon to the terminal window;
5. When you release the mouse button and option key, OS X will insert the Finder path from that window on the end of the cd command you typed earlier;
6. Hit enter.

It seems like a lot of steps, but it's pretty quick once you do it a few times.



[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: maged on Apr 22, '11 04:12:00AM

Note there is a typo in the script -- as it is now:

set pth to quoted form of POSIX path of win

last word should be "winLoc" instead of just "win"



[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: maged on Apr 22, '11 12:47:23PM

Thanks, fixed now.



[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: larsonm on Apr 24, '11 10:16:47AM

Hello!

I'm a seasoned shell script writer who needs newbie advice on modifying this applescript.

I have long been using iTerm, and I've always wanted "Open a Terminal Window Here" from the Finder.

I'm thankful for your solution, and I figured I could easily modify it to use the app "iTerm" vice "Terminal", but I get an unexpected error that I don't know how to fix:

Syntax Error. Expected end of line but found "script".

Do I have to register iTerm somehow? Does a TTY app need to be instrumented with applescript for this to work, ie: understand "do script ...". Again -- please excuse my ignorance.

I could research this with vigor and learn applescript more, but I'm sure one of you has a very quick solution for me.

Code cut/pasted here:

===============================================
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("iTerm") then
tell application "iTerm"
do script "cd " & pth & ""
end tell
else
tell application "iTerm"
activate
set twID to index of front window
end tell
tell application "iTerm"
do script "cd " & pth & "" in window twID
end tell
end if

tell application "iTerm" to activate
===============================================


Thanks in Advance,
matt



[ Reply to This | # ]
Opening new Terminal window from Finder
Authored by: feyd.rm on Apr 25, '11 08:49:05AM

I congratulate you on your efforts but I feel compelled to inform everyone that a Services workflow to do this has been available from one of Sal's sites for quite some time. Check out the "Unix Services" bundle.

http://www.macosxautomation.com/services/download/index.html


Cheers,
~feyd



[ Reply to This | # ]