V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
leecade
V2EX  ›  macOS

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

  •  
  •   leecade · 2012-06-07 16:05:47 +08:00 · 7187 次点击
    这是一个创建于 4348 天前的主题,其中的信息可能已经有所发展或是发生改变。
    完整代码:

    ```
    # 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 条回复    2016-01-14 01:21:06 +08:00
    leecade
        1
    leecade  
    OP
       2012-06-07 16:10:38 +08:00
    囧,,v2ex 是不支持 markdown 还是语法 不一样
    POPOEVER
        2
    POPOEVER  
       2012-06-07 16:13:45 +08:00
    支持 gist
    leecade
        3
    leecade  
    OP
       2012-06-07 16:17:26 +08:00
    leecade
        4
    leecade  
    OP
       2012-06-07 16:18:15 +08:00
    第一次接触 AppleScript 求指导和改进
    MartianZ
        5
    MartianZ  
       2012-06-07 18:18:23 +08:00
    其实有一个简单的办法

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

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

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

    @ratazzi Alfred 插件潜规则是:

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

    或:

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

    @lex 不能比 Go2shell 好用吧

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

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

    例如,
    open . 启动finder进入当前目录
    open ~ 启动finder进入Home目录
    donotgo
        16
    donotgo  
       2012-06-13 14:26:06 +08:00
    从finder到terminal我一直用的是cdto(http://code.google.com/p/cdto/),非常方便
    terminal到finder就像楼上说的open .
    feelinglucky
        17
    feelinglucky  
       2012-06-13 14:29:19 +08:00
    PublicFourWord
        18
    PublicFourWord  
       2016-01-14 01:21:06 +08:00   ❤️ 1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1551 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 16:28 · PVG 00:28 · LAX 09:28 · JFK 12:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.