如何更新 AppleScript 以在 iTerm3 中使用 Vim 打开文本文件

5

多年来,我一直使用以下的AppleScript在iTerm2中打开vim文本文件,但自从iTerm 2.9.2(也称为iTerm3)之后,它就无法正常工作了。有人能指导如何更新这个AppleScript,使其再次正常工作吗?

    on run {input, parameters}
    if (count of input) > 0 then
        tell application "System Events"
            set runs to false
            try
                set p to application process "iTerm"
                set runs to true
            end try
        end tell
        tell application "iTerm"
            activate
            if (count of terminals) = 0 then
                set t to (make new terminal)
            else    
                set t to current terminal
            end if
            tell t
                tell (make new session at the end of sessions)
                    exec command ("vim \"" & POSIX path of first item of input as text) & "\""
                end tell
                if not runs then
                    terminate first session
                end if
            end tell
        end tell
    end if
end run

我最初是从http://earthwithsun.com/questions/283418/how-can-i-make-terminal-vim-my-default-editor-application-in-mac-os-x复制了该脚本,但我并没有任何AppleScript经验。

非常感谢您的帮助! B


这段代码看起来像是来自 Automator,是这样吗?如果不是,你如何执行脚本? - Craig Smith
是的,在 Automator 中,将 AppleScript 保存为应用程序,然后将该应用程序与 txt 文件关联。在 Finder 中双击 txt 文件,它会在 iTerm 中的 vim 中打开。 - B-30
这个 Automator 工作流中还有其他操作吗,还是只有你在这里的单个 Run AppleScript 操作? - Craig Smith
5个回答

7

使用 @dusty 提供的链接,建议你将 Run AppleScript 操作的整个代码部分更改为以下内容:

on run {input, parameters}
    tell application "iTerm"
        activate
        if (count of windows) = 0 then
            set t to (create window with default profile)
        else
            set t to current window
        end if
        tell t
            tell current session
                write text ("vim \"" & POSIX path of first item of input as text) & "\""
            end tell
        end tell
    end tell
end run

看起来在我的机器上可以工作,但我从未使用过vim,因此不确定这是否会给您带来您想要的精确结果。

祝你好运,


需要对此进行代码签名吗?我尝试将其导出为应用程序并运行它,但我看到的是一个无操作。在控制台(Mac应用程序)中,我唯一看到的提示是3/25/16 12:26:36.000 PM kernel[0]: [90911|0] com.apple.CodeSi async. scan: #'/Applications/VI.app/Contents/Info.plist' act=0x00000002 rsn='ETYP1' ln='604' - Alex Moore-Niemi
完美运行,谢谢@craig-smith - B-30

2
新的Applescript语法不再像以前那样将terminal作为顶层对象。它已被更改以反映出在其他可脚本化应用程序中使用的常见模式:应用程序的顶层对象称为窗口,而不是终端。
因此,层次结构现在类似于以下内容:
- 包含一个或多个窗口的应用程序 - 包含一个或多个选项卡的窗口 - 包含一个会话的选项卡 https://iterm2.com/applescript.html 已更新,提供了新语法的示例。

2

我采用了Craig Smith的代码片段(非常好的答案!)并稍微调整了一下,如果已经有一个打开的窗口,就会打开一个新标签页。这样,如果您已经在iTerm中打开了类似vim的东西,就不会遇到任何问题。此外,该代码将目录更改为文件所在的目录,以使NerdTREE和模糊查找器如fzf.vim更加顺畅。

on run {input, parameters}
  set filename to POSIX path of input
  set cmd to "clear; pushd " & quote  & "$(dirname " & filename & ")" & quote  & " > /dev/null; nvim " & quote  & "$(basename " & filename & ")" & quote  & "; popd > /dev/null"

  tell application "iTerm"
    activate

    if (count of windows) = 0 then
      set t to (create window with default profile)
    else
      set t to current window
      tell t
        create tab with default profile
      end tell
    end if

    tell t
      tell current session
        write text cmd
      end tell
    end tell
  end tell
end run

请注意,当所有iTerm窗口都最小化时,此代码会崩溃,因为那时没有“当前窗口”。通过一个简单的“try”块进行修复。 - Huluk

1
我受前面答案的启发,做了一些扩展。 我的解决方案接受多个输入文件(例如来自选择)。 如果有活动窗口,脚本会在任何选项卡中搜索打开的vim会话,并在该vim实例中打开文件。如果没有,则创建一个新的iTerm选项卡或窗口。
on run {input, parameters}
    set vimCommand to "nvim -p "
    tell application "iTerm"
        activate

        if (count of windows) = 0 then
            set w to (create window with default profile)
        else
            set w to current window

            try 
                # raises error when all windows are minimized
                tell w
                    # look for tab with open vim session
                    repeat with t in tabs
                        tell t
                            if name of current session contains "vim" then

                                # open files in current vim session
                                set esc to character id 27
                                tell current session
                                    write text (esc & esc & ":silent! tablast")
                                    repeat with filename in input
                                        set filePath to quoted form of POSIX path of filename
                                        write text (":execute 'tabedit '.fnameescape(" & filePath & ")")
                                    end repeat
                                    select
                                end tell
                                select
                                return
                            end if
                        end tell
                    end repeat

                    # no existing session
                    create tab with default profile
                end tell

            on error msg
                set w to (create window with default profile)
            end try
        end if

        # open in new tab or window
        tell w
            tell current session
                set launchPaths to ""
                repeat with filename in input
                    set filePath to quoted form of POSIX path of filename
                    set launchPaths to launchPaths & " " & filePath
                end repeat
                write text (vimCommand & launchPaths)
            end tell
        end tell
    end tell
end run

几年后回来,因为我再次需要它。这个答案比原始正确答案晚了几个月,但真的很棒,因为它可以在vim已经在iTerm中打开的情况下,在一个选项卡中打开文件。如果没有,则按照原始问题的要求打开iTerm并打开vim。 - B-30

0

在Automator中类似这样的:

on run {input, parameters}
    set vim_par to POSIX path of input
    set cmd to "/usr/local/bin/vim " & quote & vim_par & quote  
    tell application "iTerm"
        tell current window
            create tab with default profile command cmd
        end tell
    end tell
end run

将文件保存为 .app 格式,然后通过它打开文件 -- 可能需要将该 .app 设置为默认的“打开方式”应用程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接