MacOS Terminal – Finding Current Finder Folder

findermacosterminal

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.

Best Answer

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)