Create a command to 'cd' to frontmost Finder window
Oct 04, '05 06:06:00AM
Contributed by: Anonymous
I've seen a few contextual menus, Automator plug-ins, and AppleScript applications written to do this, but they all require me to change my workflow. My typical workflow is:
- Open desired window in Finder
- Command-tab to Terminal
- Type a command to get me to aforementioned window
So here's my solution: Put the following snippet in your .bashrc (or .bash_profile) file to enjoy that seamless no-mouse feeling:
cdh() # cd's to frontmost window of Finder
{
cd "`osascript -e 'tell application "Finder"' \
-e 'set myname to POSIX path of (target of window 1 as alias)' \
-e 'end tell' 2>/dev/null`"
}
Close and reopen the Terminal, or type source .bashrc to activate the new command. From now on, you can just type cdh in Terminal, and automagically cd to the frontmost Finder window.
[robg adds: The code above is the result of an exchange between the author and I; it seems to work fine on both 10.3 and 10.4 machines, though I'm sure there may be better ways to achieve the same results. Note that I've broken the code onto multiple lines for a narrower display; it should work fine with a copy and paste, but if it breaks, remove the backslashes and line breaks from the cd "`... section, and make it one long line.]
Comments (24)
Mac OS X Hints
http://hints.macworld.com/article.php?story=20050924210643297