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

Click here to return to the 'Create a command to 'cd' to frontmost Finder window' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a command to 'cd' to frontmost Finder window
Authored by: suarez on Oct 09, '05 11:26:30AM
One problem with using osascript -e is that it's a tiny bit slow. I know, most people won't notice the speed difference, especially when you see the timing results below. What I use instead is:
tell application "Finder"
      POSIX path of (target of window 1 as alias)
   on error
      POSIX path of (path to desktop folder as alias)
   end try
end tell
and save that as a script in my ~/bin folder. Then, in my .bashrc file, I alias cdf to use the script with:
alias cdf='cd "`osascript ~/bin/cdf.scpt`"'
(That's back-tick, double-quote, and single-quote, from inside to out.) Rather than have the 2>/dev/null code ignore the error message, I prefer to go to the desktop if there's no open Finder window. I tried the original solution, and it ran a little slower (by a mean of 85 ms over 10 trials) than this one. I thought I had noticed a little difference, so that's why I timed it.
This, combined with the TerminalHere contextual menu, satisfies my wish-list. There's another AppleScript floating around that does the opposite of this -- opens the current folder in Finder, but this works just fine for me and it's fast:
$ open .

[ Reply to This | # ]