2

It is commonly asked how to get a Terminal prompt from a Finder window. I am asking the opposite.

I would like a command-line way of determining the working directory of the "current" Finder window.

  • 4
    What do you mean by "current" Finder window when Terminal is the active app? By definition, no Finder window has focus then... – Daniel Jan 20 '12 at 23:22
  • 1
    and more what if Finder has several windows open - what is the current window? – Mark Jan 20 '12 at 23:30
  • The actual opposite would probably be open . in Terminal... – Daniel Beck Jan 24 '12 at 7:26
  • Why don't you just drag the current folder displayed in the Finder window to the Terminal application window command line? – David Anderson Feb 5 at 14:38
10

The following command will return the path to the frontmost Finder window, if that's what you're looking for:

osascript -e 'tell application "Finder" to get the POSIX path of (target of front window as alias)'

Note that this cannot return a POSIX path if the frontmost window has no POSIX path. So if the frontmost Finder window has an ongoing/finished search, is a smart folder or other magical location (like your Mac under devices, which lists all the mounted volumes and the network).

Search/Smart folder/No Finder window returns:

execution error: Can’t make «class fvtg» of window 1 of application "Finder" into type alias. (-1700)

And trying to execute the command when viewing your Mac:

execution error: No result was returned from some part of this expression. (-2763)

  • This is a good answer. I use TotalFinder in "visor" mode (it retracts when you're not using it). If TotalFinder is retracted and I run that command, I always get the error message. If TotalFinder is pinned, I get the path as desired. Shame it doesn't work in pinned mode; oh well. Use case: I wanted to write a command-line tool to create a new file (text, word, excel, ...) in the "current" Finder window, by which I mean most-recently-focused. I've done it a different way using DTerm to open a mini-prompt from the Finder window. Works well. – nosedog Feb 28 '12 at 11:20
3

pwd: print name of current/working directory

open .: open current directory in finder

  • I'm not really sure, if this is the answer to your question. Also, what do you by getting a "Terminal prompt from a Finder window"? – gentmatt Jan 20 '12 at 23:12
  • I think you misread that one gentmatt. Edit: yup u realised. I think he means things like 'prompt here' a service to open a terminal window from Finder where the path in Terminal is preset to the finder's path. He's looking for the reverse function – Stu Wilson Jan 20 '12 at 23:13
2

This guy has a great answer http://coderwall.com/p/vp9zlw

Define the two functions:

function ff { osascript -e 'tell application "Finder"'\
 -e "if (${1-1} <= (count Finder windows)) then"\
 -e "get POSIX path of (target of window ${1-1} as alias)"\
 -e 'else' -e 'get POSIX path of (desktop as alias)'\
 -e 'end if' -e 'end tell'; };\

function cdff { pushd "`ff $@`"; };

After that you can type cdff to cd into Finder directory.

  • Hi there, I've improved the formatting for you. Check out the live preview and the formatting guide when you write posts -- they're very useful. Welcome to the site! – jmk Oct 19 '12 at 5:37
1

If you are using zsh with oh-my-zsh the requested functionality is provided by the osx-Plugin. After activating the plugin in the configuration file (usually ~/.zshrc) one can use pfd to geht the path to the current finder window and cdf to cd to it.

0

insertion location is basically the target of Finder window 1 or desktop.

f() {
    cd "$(osascript -e 'tell app "Finder"
POSIX path of (insertion location as alias)
end')"
}

There's a bug on 10.7 and 10.8 where the target and insertion location properties refer to the second frontmost window after opening a new window.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .