Topic: https://brettterpstra.com/2013/02/09/quick-tip-jumping-to-the-finder-location-in-terminal/
hide preview

What's next? verify your email address for reply notifications!

unverified 5y, 111d ago

Just in case you have Fish shell: ''' function cdf pushd (osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)') end '''

hide preview

What's next? verify your email address for reply notifications!

Marcial 6y, 32d ago

Lovely code Sir, thank you.

hide preview

What's next? verify your email address for reply notifications!

lemen 11y, 354d ago

It always changed to the oldest Finder window so I had to change front to last to get it work

tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of last Finder window as text)

hide preview

What's next? verify your email address for reply notifications!

Victor Jalencas 12y, 22d ago

>but why run an extra process when it’s this easy?

I’d have thought that running osascript also counts as an extra process.

remark link
hide preview

What's next? verify your email address for reply notifications!

ttscoff 12y, 22d ago

No.

hide preview

What's next? verify your email address for reply notifications!

Lauri Ranta 12y, 34d ago

This also prints an error if the front Finder window is in the computer or network view, exits with 1 on errors, uses local variables, and replaces $HOME with ~.

f() {
local d=$(osascript -e 'try
tell application "Finder" to POSIX path of (target of Finder window 1 as alias)
end')
[ -z "$d" ] && echo "No Finder window found" >&2 && return 1
cd "$d" && echo "${PWD/#$HOME/~}"
}

hide preview

What's next? verify your email address for reply notifications!

Philip Machanick 12y, 38d ago

As @Stuart says you can open the current directory in Terminal by

open .

Hardly long enough to be worth scripting but if you must, use this instead of the "open -a Finder ./".

remark link
hide preview

What's next? verify your email address for reply notifications!

Brett Terpstra 12y, 38d ago

The problem with `open .` is that if you're inside any kind of bundle (application, document, package, etc.) it will open that instead of Finder. I prefer to specify the application, and I'm not sure why people take such umbrage with that. It's an alias, you're just typing f either way, and my way happens to work in more circumstances. So there.

hide preview

What's next? verify your email address for reply notifications!

Nate 12y, 39d ago

in zsh you can simplify the cdf script to " cd `osascript -e 'tell application "Finder" to get POSIX path of (target of front Finder window as text)'` " and it works just as well. 

If you use it as written it still works, but for the rest of your session the directory will show up as "target" in your prompt.

remark link
hide preview

What's next? verify your email address for reply notifications!

Elliot Betancourt 12y, 29d ago

And if you use oh-my-zsh, all you need to do is enable the osx plugin (which by coincidence also uses the command "cdf") https://github.com/robbyrus... . and you get other nice things like quick-looking files from the command line

remark link parent
hide preview

What's next? verify your email address for reply notifications!

Brett Terpstra 12y, 29d ago

And if you want to quick look from the command line in bash, just alias `ql=qlmanage -p` :).

hide preview

What's next? verify your email address for reply notifications!

Brett Terpstra 12y, 38d ago

I don't know enough about zsh to know why it would substitute a variable name for a directory name, but the point of the $target variable is to allow for error catching after the AS runs. I suppose you could run the AS twice, once to check and once to perform the above, but that's overkill...

remark link parent
hide preview

What's next? verify your email address for reply notifications!

Jason Scharf 12y, 36d ago

Here is a solution that works with zsh, clearing $target fixes the issue.
# cd to the path of the front Finder windowcdf() {    target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`    if [ "$target" != "" ]; then        cd "$target"; target=""; pwd    else        echo 'No Finder window found' >&2    fi}

hide preview

What's next? verify your email address for reply notifications!

Carl 12y, 38d ago

I rewrote the script as

cdf() {
        appscript='tell application "Finder" to get POSIX path of (target of front Finder window as text)'
        cd "$(osascript -e "$appscript")"
}

because I thought it looked more elegant.

It doesn't play very nicely with Mission Control though, unfortunately. Anyone know enough AppleScript to make it go to the last window used, not the frontmost?

remark link parent
hide preview

What's next? verify your email address for reply notifications!

Brett Terpstra 12y, 38d ago

You guys missed the original, which is basically exactly what Nate has. Rob Trew noticed that it threw an inconvenient error dialog if there was no Finder window open, so we added a check to make sure the result was ok before continuing.

hide preview

What's next? verify your email address for reply notifications!

Stuart Woodward 12y, 39d ago

How about "open ."?

hide preview

What's next? verify your email address for reply notifications!