MacOS X:如何有一个方便的“在 iTerm 中打开这个文件夹”的快捷方式?

Mal*_*lax 13 finder script iterm applescript macos

我认为标题准确地说明了我想要做什么。我想要一个快捷方式,甚至是 Finder 中的一个按钮,它会启动一个新的 iTerm 选项卡并将位置更改为我在 Finder 中打开的位置。某种open .相反。

小智 10

这个applescript对我有用:

-- script was opened by click in toolbar
on run
tell application "Finder"
    try
        set currFolder to (folder of the front window as string)
    on error
        set currFolder to (path to desktop folder as string)
    end try
end tell
CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
set newWindow to false
repeat with thePath in theList
    set thePath to thePath as string
    if not (thePath ends with ":") then
        set x to the offset of ":" in (the reverse of every character of thePath) as string
        set thePath to (characters 1 thru -(x) of thePath) as string
    end if
    CD_to(thePath, newWindow)
    set newWindow to true -- create window for any other files/folders
end repeat
return
end open

-- cd to the desired directory in iterm
on CD_to(theDir, newWindow)
set theDir to quoted form of POSIX path of theDir as string
tell application "iTerm"
    activate
    delay 1
    -- talk to the first terminal 
    try
        set myterm to the first terminal
    on error
        set myterm to (make new terminal)
    end try

    tell myterm
        try
            -- launch a default shell in a new tab in the same terminal 
            launch session "Default Session"
        on error
            display dialog "There was an error creating a new tab in iTerm." buttons {"OK"}
        end try
        tell the last session
            try
                -- cd to the finder window
                write text "cd " & theDir
            on error
                display dialog "There was an error cding to the finder window." buttons {"OK"}
            end try
        end tell
    end tell
end tell
end CD_to
Run Code Online (Sandbox Code Playgroud)


Ros*_*oss 8

使用此页面上的其他答案,我创建了一个可以拖动到查找器任务栏中的应用程序。

你可以从这里下载:https : //github.com/rc1/iTermTo


For*_*lon 5

AppleScript有一个Open Terminal Here,您应该可以修改它以调用 iTerm。这个MacOSXHints 帖子也应该有帮助。

(我不在我的 Mac 上,否则我会测试它。)


Bra*_*pit 5

这是从 3.1.0 版本开始内置于 iTerm2 中的。

要使用该功能:
在 Finder 中右键单击文件夹 -> 服务 -> 在此处新建 iTerm2 窗口

注:Services子菜单位于右键菜单的最底部。

参考
在此链接中单击Show Older Versions,然后在iTerm2 3.1.0下单击Show Changelog并查找services,您会发现:

添加对查找器服务的支持。您可以在 Finder 中右键单击以在该位置启动 iTerm2。