Finder中打开iTerm

通过Automator可以自意义services,services可以认为是扩展菜单,为应用添加系统未曾提供的功能。本文通过Automator为Finder添加一个扩展菜单,实现当在Finder中选中一个文件夹时,可通过右键菜单直接打开iTerm,并定位到对应目录下。

添加Service

新建一个Automator,类型选择Service。

使用Automator新建Service

添加两个Action,第一个为Get Selected Finder Items,这个是第二个Action的输入参数,当用鼠标选中文件时,通过这个Action可以获得文件夹路径等信息。

第二个Action为Run AppleScript,Action如下图所示:

Finder添加打开iTerm功能

在第二个Action中添加AppleScript,这样当执行service时,便会触发AppleScript执行,AppleScript如下:

-- Adapted from these sources:
-- http://peterdowns.com/posts/open-iterm-finder-service.html
-- https://gist.github.com/cowboy/905546
-- 
-- Modified to work with files as well, cd-ing to their container folder
on run {input, parameters}
  tell application "Finder"
        set my_file to first item of input
        set filetype to (kind of (info for my_file))
        -- Treats OS X applications as files.  To treat them as folders, integrate this SO answer:
        -- http://stackoverflow.com/a/6881524/640517
        if filetype is "Folder" or filetype is "Volume" then
            set dir_path to quoted form of (POSIX path of my_file)
        else
            set dir_path to quoted form of (POSIX path of (container of my_file as string))
        end if
    end tell
    CD_to(dir_path)
end run

on CD_to(theDir)
    tell application "iTerm"
        activate
        set go_dir to "cd " & theDir
        set newWindow to (create window with default profile)
        tell current session of first window
            write text go_dir
        end tell
    end tell
end CD_to

完成上述步骤后,保存Service,命名为:Open iTerm Here

测试Service

打开Finder,随便选中一个文件夹,然后右击,选择services -> Open iTerm Here,一切顺利的话,可正确打开iTerm。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这是我这段时间写的关于Mac Finder的一系列知识和操作技巧,现在整理出来,希望大家能够喜欢。我也将会继续奉献...
    Mac高级玩家阅读 116,102评论 11 236
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,523评论 0 17
  • 前些天看到一篇讲增强xcode开发效率的文章,文末提到Automator的妙用。虽然自己以前也简单折腾过Autom...
    felix9阅读 4,302评论 0 0
  • 连载《传奇》第一章 星夜转移连载《传奇》第二章 子容潜去连载《传奇》第三章 莲珠岛国连载《传奇》第四章 平...
    ARUNANHUI阅读 599评论 2 3