Open Terminal Here Finder Keyboard Command
Mac OS X — 7 Aug 2008 08:38 — 1378 days ago

A while ago I published a Finder toolbar AppleScript called “Open Terminal Here” to open a terminal window in the directory displayed by the frontmost Finder window or the Finder selection or the files/folders dropped onto the icon.

However, these days I find myself using a similar script triggered by a keyboard shortcut much more often. I assigned it to Cmd-T which is very convenient and quick. As always, I use FastScripts to do the keyboard launching.

Here’s the script:

tell application "Finder"
	set sel to selection
	if (count sel) > 0 then
		set myTarget to item 1 of sel
	else if (count window) > 0 then
		set myTarget to target of window 1
	else
		set myTarget to path to desktop folder
	end if
	my openTerminal(myTarget)
end tell

on openTerminal(location)
	set location to location as alias
	set the_path to POSIX path of location
	repeat until the_path ends with "/"
		set the_path to text 1 thru -2 of the_path
	end repeat
	
	set cmd to "cd " & quoted form of the_path & " && echo $'\\ec'"
	
	tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
	
	tell application "Terminal"
		activate
		if terminalIsRunning is true then
			do script with command cmd
		else
			do script with command cmd in window 1
		end if
	end tell
end openTerminal
Open this script in Script Editor

Comments
Posted by ~HP on 7 Aug 2008 19:05

I think, you can give a look here:
http://hayne.net/MacDev/Bash/aliases.bash

especially to this:
# cdf: cd's to frontmost window of Finder
cdf ()

Posted by Rob... on 9 Aug 2008 18:42

This doesn't work for me. When I try to compile in Script Editor, I get the message:

"Expected end of line but found number."

with the "1" in the line "set the_path to texts 1 thru -2 of the_path" highlighted.

Any ideas?

Thanks,

Rob...

Posted by Marc on 10 Aug 2008 12:36

Rob, you’re right, this doesn’t compile in Script Editor, I use a different AppleScript editor (ScriptDebugger) and it works there, weird.

It seems that ScriptDebugger converted “text” to “texts” automatically. If I remove the “s” then it works for me. I changed the listing above accordingly.

Posted by Rob... on 13 Aug 2008 21:26

Thanks Marc!

I'll really have to learn applescript soon...

Regards,

Rob...

Posted by Jon on 19 Aug 2008 11:57

Just a pointer to yet another nifty "Open Terminal Here in Finder" app:

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

Posted by Marc on 19 Aug 2008 14:09

Jon: Thanks for the hint, I didn't know about that one, looks good.

It does not handle my test directory though, and it would be nice if it would clear the terminal history in a way that removes the cd command from the scrollback history (I send an echo $'\ec' command to the shell)

My test directory has lots of special characters in its name and I use it to test my scripts against it. The name is

testö'"/`aぎsd

Posted by Christian H. on 20 Aug 2008 22:38

Maybe Automator to the rescue?

http://codesnippets.joyent.com/posts/show/1623

Powered By blojsom