In Terminal, can I find out the current Finder folder?
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.
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)
pwd
: print name of current/working directory
open .
: open current directory in finder
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.
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.