3

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

5 Answers 5

13

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)

3
  • 1
    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
    Commented Feb 28, 2012 at 11:20
  • Thanks for the answer. A quick question from beginner, where did you find such OS-specific command. For example, a beginner may never hear such command osascript, so do you have a more systematic way to search what command you want other than randomly googling?
    – Jason
    Commented Apr 2, 2021 at 0:50
  • 1
    @Jason The osascript is indeed OS-specific – it tells the operating system to run an AppleScript (it's the command-line bridge between the GUI-based AppleScript and the Unix shell). MacOS X (well, now we're up to version 11) is the fusion of two longstanding operating systems: Unix and Mac OS, and this particular command links their two distinct scripting languages. So this particular command is deeply rooted in the dual citizenship of this OS.
    – Daniel
    Commented Apr 2, 2021 at 2:01
4

pwd: print name of current/working directory

open .: open current directory in finder

2
  • 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
    Commented Jan 20, 2012 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
    Commented Jan 20, 2012 at 23:13
4

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.

1
  • 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
    Commented Oct 19, 2012 at 5:37
2

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 .