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

Open a Terminal window at current Finder selection Desktop
I often want to do something in OS X and find that I need root when I have navigated to a specific location in the Finder. So then I would have to open the terminal and navigate to that same location (or drag the folder while holding command-option onto the Terminal icon in the dock).

Well, I finally got around to creating an Applescript that does it for me, here it is:
on run
tell application "Finder"
try
activate
set frontWin to folder of front window as string
set frontWinPath to (get POSIX path of frontWin)
tell application "Terminal"
activate
do script with command "cd \"" & frontWinPath & "\""
end tell
on error error_message
beep
display dialog error_message buttons¨
{"OK"} default button 1
end try
end tell
end run
Save the script as an application and drop it in the Finder toolbar (or in the Scripts folder or use one of the available utilities to assign it a keyboard shortcut) and then each window has a handy link to the terminal which will open a new window navigated to that folder. This saves duplication of effort and long drag and drop operations.
    •    
  • Currently 5.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[70,030 views]  

Open a Terminal window at current Finder selection | 15 comments | Create New Account
Click here to return to the 'Open a Terminal window at current Finder selection' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Doing the opposite
Authored by: nazgul on Apr 26, '02 10:07:55AM

I don't often find a need to open a shell window on the current finder selection, however I often want to open a finder window on the current shell location, and that one's much easier, but it might be worth mentioning.

open .




[ Reply to This | # ]
Doing the opposite
Authored by: porkchop_d_clown on Apr 26, '02 01:45:03PM

Nice! I was just wondering how to do that...



[ Reply to This | # ]
Fatal Flaw in this AppleScript
Authored by: Anonymous on Apr 26, '02 09:58:57PM

I think this is a very nice idea, but it unfortunately has a flaw that is fatal, at least in my experience: if the program your cursor happens to be one that can be further opened by selecting "Show Package Contents" off the contextual menu, the script opens up not the directory the program is in, but the program itself as a directory.

For instance, let your cursor rest on "Script Editor" and click the button, and you won't be opened to /Applications/AppleScript, you'll be opened to /Applications/AppleScript/Script Editor.app.

One can simply drag the title of the window to a Terminal window. It's a drag-and-drop interface, yes, but doesn't have this liability. (If, however, this liability can be solved, the proposed solution here is far more convenient, I agree.)



[ Reply to This | # ]
Open Terminal Here
Authored by: Elander on Apr 27, '02 06:37:05AM
Marc Liyanage has written a script called "Open Terminal Here" that actually works (without the flaw described earlier about packages, as far as I can tell). It is freeware, and I have been using it for a long time with no bad side effects (except perhaps an increasing laziness due to the ease of use;-) You can find it on his site, along with a bunch of other good stuff.

[ Reply to This | # ]
Open Terminal Here
Authored by: TomWoozle on Apr 27, '02 07:29:46AM

Hehe - could have told me before I went on an Applescript crash course!! I will download 'Open Terminal Here' when I get home - sounds excellent!



[ Reply to This | # ]
A Simpler Way
Authored by: ershler on Apr 27, '02 10:47:19PM

To achieve the same end, open a terminal window, type cd"space" and then click down on the folder icon at the top of the window where you need to be. As soon as the folder turns black, drag it to the terminal window, and type return. Presto you're there!



[ Reply to This | # ]
A Simpler Way
Authored by: osxpounder on Jun 17, '03 04:19:44PM

I never noticed if this changed with newer OSX versions, but at least in my 10.2.6 box, the icon never turns black, or changes at all.

I just hold down for one full second, then drag. You have to hold down longer than you usually do before dragging things, for this to work, but even a little bit longer will do. You'll know when you've got it--either the window moves [if it does, start over and hold longer from now on], or the icon moves [in which case, drag it where you need it].

You can also create aliases or copies this way. After you've started dragging, you hold down either Option, to copy, or Cmd-Option, to make an alias. In fact, just using the Cmd alone will move the folder. Just be sure to wait until you're dragging to hold down those keys; I have made it a habit to always drag that way [drag first, then add keys], and it works great for me.


---
--
osxpounder



[ Reply to This | # ]
I like this
Authored by: sjonke on Apr 28, '02 05:00:01PM

I have used Open Terminal Here but really hate the weird flurry of mess it makes when I use it - you see text flying all about and why? Some needless perl script gets run that does all that. This is much cleaner. I confess to having copied the icon from Open Terminal Here, though, and pasting it on this script. :) The .pkg thing is no big deal to me and I think it should be fairly easy to correct for that once I get some time to mess around with the script a bit.



[ Reply to This | # ]
I like this
Authored by: liyanage on Apr 29, '02 01:57:36PM
I don't like the ugly Perl line in my "Open Terminal Here" script either. My script actually worked like this one in an earlier version.
The reason I changed it to use Perl was to support weird directory paths. Mine will now handle almost anything you can throw at it, including the contents of packages etc.
I have a test folder called te'st"ö te%s`t. It has all the ugly special cases in it that broke earlier versions of my script.
Glad you like my icon though :-)

[ Reply to This | # ]
Fix for flaw
Authored by: sigpoggy on Dec 04, '02 09:39:31PM

Add the following just before the tell terminal line:

repeat until frontWinPath ends with "/"
set frontWinPath to characters 1 thru -2 of frontWinPath as string
end repeat



[ Reply to This | # ]
Open a Terminal window at current Finder selection
Authored by: allanmarcus on Aug 19, '04 10:26:32AM
here's a modified version of Marc Liyanage's Open Terminal Here that does use the perl script. This one works more smoothly for both drag and drop and a click. I dragged in to my toolbar and it works great. It does have the "issue" that if you drag an app to it, it will open in the ".app" directory, but that's the expected behavior.

(*

	Open Terminal Here
	
	A toolbar script for Mac OS X 10.3
	
	Written by Marc Liyanage

	
	See http://www.apple.com/applescript/macosx/toolbar_scripts/ for
	more information about toolbar scripts.
	
	See http://www.entropy.ch/software/applescript/ for the latest
	version of this script.
	
	
	History:
	18-AUG-2004: Version 2.0 by Allan Marcus. uses posix path	
	30-OCT-2001: Version 1.0, adapted from one of the example toolbar scripts
	30-OCT-2001: Now handles embedded single quote characters in file names
	30-OCT-2001: Now handles folders on volumes other than the startup volume
	30-OCT-2001: Now handles click on icon in top-level (machine) window
	31-OCT-2001: Now displays a nicer terminal window title, courtesy of Alain Content
	11-NOV-2001: Now folders within application packages (.app directories) and has a new icon
	12-NOV-2001: New properties to set terminal columns and rows as the Terminal does not use default settings
	14-NOV-2001: Major change, now handles 8-bit characters in all shells, and quotes and spaces in tcsh
	18-NOV-2001: Version 1.1: Rewrite, now uses a temporary file  ~/.OpenTerminalHere to communicate
	the directory name between AppleScript and the shell because this is much more reliable for 8-bit characters
	
*)

property terminal_rows : 24
property terminal_columns : 90
property debug : true

-- when the toolbar script icon is clicked
--
on run
	tell application "Finder"
		activate
		try
			set this_folder to (the target of the front window) as alias
		on error
			set this_folder to startup disk
		end try
		my process_item(this_folder)
	end tell
end run

-- This handler processes folders dropped onto the toolbar script icon
--
on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to item i of these_items
		my process_item(this_item)
	end repeat
end open

-- this subroutine processes does the actual work
--
on process_item(this_item)
	set thePath to quoted form of POSIX path of this_item
	tell application "Terminal"
		activate
		do script with command "cd " & thePath
		tell window frontmost
			set custom title to this_item
		end tell
	end tell
end process_item




[ Reply to This | # ]
Open a Terminal window at current Finder selection
Authored by: klktrk on Aug 21, '04 12:09:54AM

Too bad you can't use this script with iTerm. I tried. Doesn't work.



[ Reply to This | # ]
Open a Terminal with either iTerm or Terminal
Authored by: klktrk on Aug 21, '04 02:18:59AM

Okay, so I figured out how to do it with iTerm:

(*

Open Terminal Here

A toolbar script for Mac OS X 10.3

Written by Marc Liyanage


See http://www.apple.com/applescript/macosx/toolbar_scripts/ for
more information about toolbar scripts.

See http://www.entropy.ch/software/applescript/ for the latest
version of this script.


History:
20-AUG-2004: Version 2.0.1 by Kristofer Widholm, can work with either iTerm or Terminal
18-AUG-2004: Version 2.0 by Allan Marcus. uses posix path
30-OCT-2001: Version 1.0, adapted from one of the example toolbar scripts
30-OCT-2001: Now handles embedded single quote characters in file names
30-OCT-2001: Now handles folders on volumes other than the startup volume
30-OCT-2001: Now handles click on icon in top-level (machine) window
31-OCT-2001: Now displays a nicer terminal window title, courtesy of Alain Content
11-NOV-2001: Now folders within application packages (.app directories) and has a new icon
12-NOV-2001: New properties to set terminal columns and rows as the Terminal does not use default settings
14-NOV-2001: Major change, now handles 8-bit characters in all shells, and quotes and spaces in tcsh
18-NOV-2001: Version 1.1: Rewrite, now uses a temporary file ~/.OpenTerminalHere to communicate
the directory name between AppleScript and the shell because this is much more reliable for 8-bit characters

*)

property terminal_rows : 24
property terminal_columns : 90
property debug : true
property terminalApp : "iTerm" --set to "Terminal" if you'd rather use that.
-- when the toolbar script icon is clicked
--
on run
tell application "Finder"
activate
try
set this_folder to (the target of the front window) as alias
on error
set this_folder to startup disk
end try
my process_item(this_folder)
end tell
end run

-- This handler processes folders dropped onto the toolbar script icon
--
on open these_items
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
my process_item(this_item)
end repeat
end open

-- this subroutine processes does the actual work
--
on process_item(this_item)
set thePath to quoted form of POSIX path of this_item
if terminalApp is equal to "iTerm" then
tell application "iTerm"
activate
try
set firstTerminal to (the first terminal)
on error
set firstTerminal to (make new terminal)
end try
tell firstTerminal
launch session "Default Session"


tell the last session

-- write some text
write text "cd " & thePath
end tell
end tell
end tell
else if terminalApp is equal to "Terminal" then
tell application "Terminal"
activate
do script with command "cd " & thePath
tell window frontmost
set custom title to this_item
end tell
end tell
end if
end process_item



[ Reply to This | # ]
Open a Terminal window at current Finder selection
Authored by: taxi on Mar 11, '05 02:45:33AM

I don't like how long it takes for an AppleScript to start up, and then execute, then quit.

Take the code that is being run, and add the following above it:

#! /bin/sh
osascript <<END

and the word END on it's own line below it, and save it to a text file, which ends in .app (rather than .txt).

Now, the script starts almost instantly. No bouncing Dock icon. Much nicer. Might write this up as a hint of it's own...



[ Reply to This | # ]
Open a Terminal window at current Finder selection
Authored by: apta on Apr 26, '11 07:25:01PM

Naming the osacript with a ".app" suffix as indicated by taxi did not produce the intended result. The file is interpreted to be a Classic application and when double clicked a message comes up that the Classic environment is no longer supported.

How do I get this file to launch correctly?



[ Reply to This | # ]