MacOS X:如何使用方便的“在iTerm中打开此文件夹”快捷方式?


12

我认为标题正好说明了我想做什么。我想要Finder中的快捷方式甚至按钮,以启动新的iTerm选项卡并将位置更改为我在Finder中打开的位置。某种open .反向。:-)

谢谢,马拉克斯

Answers:



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

1
我认为这应该是公认的答案。
dhilipsiva 2013年

8

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

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


1
极好的工作!完美运作。这应该是公认的答案。
rcd

1
我同意-似乎运作良好。下载压缩文件。将应用程序拖到Applications文件夹进行安装。将应用程序拖动到查找器工具栏上,以获取方便的快捷方式。
justingordon 2013年

3

从3.1.0版本开始,它内置在iTerm2中。

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

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

参考
在此链接上,单击“ 显示旧版本”,然后在“ iTerm2 3.1.0”下单击“ 显示更改日志”并查找服务,您将发现:

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



1

出于完整性考虑,在找到此问题之前,对我有用的是:

  • new_tab.sh(由bash脚本发行的AppleScript)修改为仅AppleScript的解决方案。
  • 然后从Applescript Editor-> File-> Export-> File Format = .app
  • 将拖放.app到Finder的工具栏。

这将产生一个Finder工具栏按钮,该按钮将在新iTerm2选项卡中打开当前目录。XtraFinder提供了这样的按钮,但它会打开新窗口。

此处可以找到使用服务的类似解决方案,该解决方案链接到更多相关的AppleScript解决方案:

我改编的AppleScript是:

try
    tell application "iTerm2"
        tell the last terminal
            launch session "Default Session"
            tell the last session
                tell i term application "Finder"
                    set cur_dir to (the target of the front Finder window) as string
                end tell
                set cur_dir to POSIX path of cur_dir
                write text "cd " & cur_dir
            end tell
        end tell
     end tell
end try

此解决方案在与按钮相关的线程中进行了评论。

感谢上面的iTermTo回答。


1

我猜这是因为iTerm的内部结构已发生变化,但是没有一种解决方案适合我。下面的代码是做什么的:

tell application "Finder"
    set cur_dir to POSIX path of ((the target of the front Finder window) as string)
end tell
tell application "iTerm"
    tell (create window with default profile)
        write current session text "cd " & quoted form of cur_dir
    end tell
end tell

或使用Automator作为查找器服务:

on run {input, parameters}
    tell application "Finder"
        set cur_dir to POSIX path of (input as string)
    end tell
    tell application "iTerm"
        tell (create window with default profile)
            write current session text "cd " & quoted form of cur_dir
        end tell
    end tell
end run

0

使用iTerm:

在“ Iterm首选项”和“配置文件”选项卡下,转到“常规”子选项卡,将“工作目录”设置为“重用上一会话的目录”。


0

这是一个简化的脚本,该脚本始终打开一个新选项卡(如Bulljit的脚本):

try
    tell application "Finder"
        if number of Finder windows is 0 then
            set p to POSIX path of (desktop as alias)
        else
            set p to POSIX path of (target of Finder window 1 as alias)
        end if
    end tell
    tell application "iTerm"
        reopen
        tell current terminal
            tell (launch session "Default Session")
                write text "cd " & quoted form of p
            end tell
        end tell
        activate
    end tell
end try

如果您希望脚本重用现有的选项卡,请使用以下内容替换该tell current terminal块:

tell current session of current terminal
    write text "cd " & quoted form of p
end tell

但是,例如,如果当前会话很忙或者正在运行一个少或vim的进程,那将无法正常工作。

将脚本包装在try块中会使它静默失败。reopen如果没有可见的窗口,或者例如仅打开首选项窗口,则打开一个新的终端窗口。Finder还具有一个insertion location属性,通常target of Finder window 1是桌面属性。但是在10.7及更高版本中存在一个错误,该错误通常指的是除最前面的窗口以外的其他窗口。

Bulljit脚本的一些潜在问题:

  • 它有一秒钟的延迟。我不知道某些事情是否需要它,但是在测试此脚本时我不需要任何延迟。
  • 如果您已将iTerm设置为全屏打开新窗口,但是没有打开的窗口,则会打开非全屏窗口。
  • 它告诉Finder获取front windowwindow 1)的路径,该路径可以是信息窗口或首选项窗口。Finder window 1始终是文件浏览器窗口。
  • /如果最前面的Finder窗口正在显示没有路径的视图(如“网络”视图),它将目录更改为。

我更喜欢只使用这样的函数:

cf () {
  c "$(osascript -e 'tell application "Finder"
    POSIX path of (target of Finder window 1 as alias
  end tell)' 2> /dev/null)"
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.