leecade
V2EX  ›  macOS

搞定直接在终端中 cd 到 Finder 目录,兼容(iTerm2/TotalFinder)

  •  
  •   leecade · Jun 7, 2012 · 8516 views
    This topic created in 5264 days ago, the information mentioned may be changed or developed.
    完整代码:

    ```
    # 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
    18 replies  •  2016-01-14 01:21:06 +08:00
    leecade
    leecade
    OP
    Jun 7, 2012
    1
    囧,,v2ex 是不支持 markdown 还是语法 不一样
    POPOEVER
    POPOEVER
    Jun 7, 2012
    2
    支持 gist
    leecade
    leecade
    OP
    Jun 7, 2012
    4
    第一次接触 AppleScript 求指导和改进
    MartianZ
    MartianZ
    Jun 7, 2012
    5
    其实有一个简单的办法

    打开bash 输入cd+空格 然后把finder当前窗口标题栏中间的小图标拖拽过去就好了

    当然还是比写脚步麻烦,但是适用于任何时候获得目录地址
    leecade
    leecade
    OP
    Jun 7, 2012❤️1
    6
    @MartianZ 就是要规避来回切换操作,不然 Go2shell 也是个办法
    ratazzi
    ratazzi
    PRO
    Jun 7, 2012
    7
    没搞懂 “检查当前状态是否可以获取路径” 这段起什么作用
    Alfred 插件里只有这一行代码

    cd "`osascript -e 'tell application "Finder" to set theFolder to POSIX path of (target of window 1 as alias)'`"
    waterye
    waterye
    Jun 7, 2012
    8
    切来切去麻烦,宁愿打几个tab
    yuelang85
    yuelang85
    Jun 7, 2012
    9
    直接把目录拖进终端不行?
    lex
    lex
    Jun 7, 2012
    10
    上次ForkLift 99美分的时候买了一个,设置了一个快捷键command+option+o直接在terminal里打开当前目录,无比好用。
    leecade
    leecade
    OP
    Jun 8, 2012
    11
    @waterye 就是要避免切来切去啊,不需要 finder 焦点,隐藏、最小化都可以

    @ratazzi Alfred 插件潜规则是:

    Finder 不能处于隐藏状态(commond+h)

    或:

    TotalFinder 不能处于最小化状态(commond+m)

    @lex 不能比 Go2shell 好用吧

    1. 得切换焦点到 finder 才能快捷键

    2. 一直没找到第3方调 iTerm2 时,打开新 tab 而不是 window 的 api,有请告知
    zhufenggood
    zhufenggood
    Jun 8, 2012
    12
    直接用 Go2Shell 不就行了么,支持terminal iterm2
    http://itunes.apple.com/us/app/go2shell/id445770608?mt=12
    zhensun
    zhensun
    Jun 8, 2012
    13
    @leecade, 可以 get last window 或者 make window,然后 set mysession to (make new session at the end of sessions)。iTerm 网站上有一些例子。
    lex
    lex
    Jun 8, 2012
    14
    @leecade @zhufenggood Go2Shell的图标和Finder严重不搭,有没有可以换的图标?
    vixvix
    vixvix
    Jun 8, 2012
    15
    在term下"open 目录"就可以启动finder然后进入该目录。

    例如,
    open . 启动finder进入当前目录
    open ~ 启动finder进入Home目录
    donotgo
    donotgo
    Jun 13, 2012
    16
    从finder到terminal我一直用的是cdto(http://code.google.com/p/cdto/),非常方便
    terminal到finder就像楼上说的open .
    feelinglucky
    feelinglucky
    Jun 13, 2012
    17
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5029 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 08:10 · PVG 16:10 · LAX 00:10 · JFK 03:10
    ♥ Do have faith in what you're doing.