如何更新 applescript 以在 iTerm3 中打开 vim 中的 txt 文件

How to update applescript to open txt files in vim in iTerm3

多年来,我一直在使用以下 AppleScript 在 iTerm2 的 vim 中打开 txt 文件,但自 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

新的 Applescript 语法不像以前那样将 terminal 作为顶级对象。它已被更改以反映整个 OS 中用于其他脚本应用程序的更多通用模式:应用程序的顶级对象称为 windows,而不是终端。

所以层次结构现在是这样的:

  • 应用程序包含一个或多个
    • windows包含一个或多个
      • 标签包含
        • 届会

https://iterm2.com/applescript.html 已使用新语法示例进行了更新。

使用@dusty 的 link,我建议您将 运行 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,所以我不确定这是否能准确地满足您的需求。

祝你好运,

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 设为默认 "Open with" 应用。

我采用了 Craig Smith 的代码片段(很好的答案!)并对其进行了一些微调,以便在已经有一个打开的选项卡时打开一个新选项卡 window。这样,如果您已经在 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

我从前面的回答中得到启发,做了一些扩展。 我的解决方案接受多个输入文件(例如来自选择)。 如果有活动的 window,脚本会在任何选项卡中搜索打开的 vim 会话,并打开该 vim 实例中的文件。如果有 none,它会创建一个新的 iTerm 选项卡或 window。

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