Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac Terminal change directory to current front finder window [closed]

Title says it all. I just want to know what command to enter to change the working directory to the directory of whatever is open in the front finder window.

like image 677
Tomulent Avatar asked Jan 13 '23 13:01

Tomulent


1 Answers

My answer is based off an answer to a similar question about how to get the path of the front-most window in Objective-C and Cocoa (which is apparently from a Mac OS X Hints tip which might have been your exact question). I've tested the command and it will work directory paths containing spaces too.

cd "`osascript -e 'tell application "Finder" to POSIX path of (target of window 1 as alias)'`"

Using bash? Add this function to your bash profile.

cdf () {
  finderPath=`osascript -e 'tell application "Finder"
                               try
                                   set currentFolder to (folder of the front window as alias)
                               on error
                                   set currentFolder to (path to desktop folder as alias)
                               end try
                               POSIX path of currentFolder  
                            end tell'`;
  cd "$finderPath"
}
like image 177
Linville Avatar answered Jan 30 '23 06:01

Linville