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

时间:2013-08-27 03:58:28

标签: macos unix terminal

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

3 个答案:

答案 0 :(得分:6)

我的回答是基于类似question about how to get the path of the front-most window in Objective-C and Cocoa的答案(显然是来自Mac OS X Hints提示,这可能是您的确切问题)。我已经测试了命令,它也会处理包含空格的目录路径。

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

使用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"
}

答案 1 :(得分:2)

使用Finder的insertion location属性也非常有效 - 这会将工作目录更改为Finder中当前活动的文件夹(最后一个窗口或桌面空间):

cd "`osascript -e "tell application \\"Finder\\" to get POSIX path of (insertion location as text)"`"

或者,将以下行添加到bash配置文件中(〜/ .bash_profile):

alias cdf='cd "`osascript -e "tell application \\"Finder\\" to get POSIX path of (insertion location as text)"`"'

答案 2 :(得分:0)

在Yosemite中,我只是将文件夹从Finder窗口拖到我的底座上的终端图标顶部,终端打开到该文件夹​​。整洁不是吗?无需脚本或服务。

相关问题