【问题标题】:How to update applescript to open txt files in vim in iTerm3如何更新applescript以在iTerm3中的vim中打开txt文件
【发布时间】:2016-02-27 05:43:04
【问题描述】:

多年来,我一直使用以下 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 经验。

非常感谢任何帮助! 乙

【问题讨论】:

  • 这段代码看起来像是来自 Automator,对吗?如果没有,如何执行脚本?
  • 是的,在 Automator 中,将 AppleScript 保存为应用程序,然后将该应用程序与 txt 文件相关联。在finder中双击txt文件,在iTerm的vim中打开。
  • 此 Automator 工作流程中是否还有其他操作,或者只是您在此处拥有的单个 Run AppleScript?

标签: vim applescript iterm2


【解决方案1】:

使用 @dusty 的链接,我建议您将 Run AppleScript Action 的整个代码部分更改为:

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'
  • 完美运行,谢谢@craig-smith
【解决方案2】:

新的 Applescript 语法不像以前那样将 terminal 作为顶级对象。它已被更改以反映整个操作系统中用于其他可编写脚本的应用程序的更多常见模式:应用程序的顶级对象称为窗口,而不是终端。

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

  • 包含一个或多个的应用程序
    • 包含一个或多个的窗口
      • 标签包含
        • 会话

https://iterm2.com/applescript.html 已更新为新语法示例。

【讨论】:

    【解决方案3】:

    我采用了 Craig Smith 的代码 sn-p(很好的答案!),如果已经有一个打开的窗口,我对其进行了一些调整以打开一个新选项卡。这样,如果您已经在 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 窗口最小化时,这会崩溃,因为那时没有 current window。用一个简单的try 块修复。
    【解决方案4】:

    我从之前的回答中汲取了灵感,并做了一些扩展。 我的解决方案接受多个输入文件(例如来自选择)。 如果有一个活动窗口,脚本会在任何选项卡中搜索打开的 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 中打开,它会在 vim 的选项卡中打开文件。如果没有,它会根据原始问题打开 iTerm 并打开 vim。
    【解决方案5】:

    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 设为默认的“打开方式”应用。

    【讨论】:

      猜你喜欢
      • 2016-06-28
      • 1970-01-01
      • 2014-07-04
      • 2017-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      相关资源
      最近更新 更多