完整代码:
```
# cd Finder path
# 兼容 Finder/TotalFinder、Terminal/iTerm2,兼容 Finder 处于隐藏或最小化状态
# TODO:在处理隐藏或最小化状态时没有找到合适API,实现太绕
function cd.() {
# 检查 Finder 是否执行
if [ "`osascript -e 'tell application "System Events" to "Finder" is in (get name of processes)'`" = "true" ]; then
# 检查当前状态是否可以获取路径
if [ "`osascript -e 'tell application "Finder" to get collapsed of front window' 2>/dev/null`" != "false" ]; then
if [ "`osascript -e 'tell application "System Events" to "TotalFinderCrashWatcher" is in (get name of processes)'`" = "true" ];then
# 隐藏TotalFinder(最小化(commond+m)报错)
open .
osascript -e 'tell application "System Events" to tell process "Finder" to keystroke "w" using {command down}' -e 'tell application "System Events" to tell process "Finder" to keystroke "h" using {command down}'
else
# 最小化Finder(隐藏状态(commond+h) 报错)
finderState=`osascript -e 'tell application "System Events" to set visible of application process "Finder" to true' -e 'tell application "Finder" to set collapsed of front window to true' 2>/dev/null`
fi
fi
finder=`osascript -e 'tell application "Finder" to set curName to (POSIX path of (target of front window as alias))' 2>/dev/null`
# 检查返回值是否为空字符串
if [ -z "$finder" ]; then
echo "Failed to find \"Finder\""
else
echo "$finder"
cd "$finder"
fi
else
echo "\"Finder\" is not running"
fi
}
```
将上面代码添加至 ~/.bashrc 后,即可享用cd.(结合open .来去自如了)
-------------
> 研究过程:
http://www.jsser.com/t/75
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/38686
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.