Mac终端将目录更改为当前前端查找器窗口

Tom*_*ent 3 unix macos terminal

标题说明了一切.我只想知道输入什么命令将工作目录更改为前端查找器窗口中打开的任何目录.

Lin*_*lle 6

我的答案是基于一个类似问题的答案,该问题是关于如何在Objective-C和Cocoa中获取最前面窗口的路径(这显然来自Mac OS X Hints提示,这可能是您的确切问题).我测试了命令,它也将工作包含空格的目录路径.

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

使用bash?将此功能添加到您的bash配置文件中.

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"
}
Run Code Online (Sandbox Code Playgroud)