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 openTerminalOpen this script in Script Editor
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...
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.
Thanks Marc!
I'll really have to learn applescript soon...
Regards,
Rob...
Just a pointer to yet another nifty "Open Terminal Here in Finder" app:
http://code.google.com/p/cdto/
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
Maybe Automator to the rescue?
http://codesnippets.joyent.com/posts/show/1623
Trackback URL for this entry: http://www.entropy.ch/blog/Mac+OS+X/2008/08/07/Open-Terminal-Here-Finder-Keyboard-Command.html?tb=y
|













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 ()