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

如何在iTerm3中更新applescript以在vim中打开txt文件,vim,applescript,iterm2,Vim,Applescript,Iterm2,多年来,我一直在使用下面的AppleScript在iTerm2的vim中打开txt文件,但自从iTerm2.9.2(又名iTerm3)之后,它就坏了。有人能建议如何更新此AppleScript使其再次工作吗 on run {input, parameters} if (count of input) > 0 then tell application "System Events" set runs to false

多年来,我一直在使用下面的AppleScript在iTerm2的vim中打开txt文件,但自从iTerm2.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
我最初是从中复制脚本的,但我没有任何AppleScript经验

非常感谢您的帮助!
B

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

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

  • 包含一个或多个应用程序的应用程序
    • 包含一个或多个窗口的窗口
      • 包含
        • 会议

已使用新语法的示例进行了更新。

使用@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,所以我不确定这是否能准确地满足您的需求


祝你好运,

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

我采用了Craig Smith的代码片段(很好的答案!),并对其进行了一些调整,以便在已经有打开窗口的情况下打开一个新选项卡。这样,如果您已经在iTerm中打开了类似vim的东西,您就不会遇到任何问题。此外,这段代码将目录更改为文件的目录,以使NerdTREE和fuzzy finder(如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

我在之前的anwers中获得了灵感,并做了一些扩展。 我的解决方案接受多个输入文件(例如,来自一个选择)。 如果存在活动窗口,脚本将在任何选项卡中搜索open 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

这个代码看起来像来自Automator,对吗?如果没有,如何执行脚本?是的,在Automator中,将AppleScript另存为应用程序,然后将该应用程序与txt文件关联。双击finder中的txt文件,它将在iTerm中的vim中打开。此Automator工作流中是否有任何其他操作,或者只是您这里的单次运行AppleScript?是否需要对其进行代码签名?我尝试将其导出为一个应用程序并运行它,我在控制台(mac应用程序)中看到了一个禁止操作的提示。我看到的唯一提示是
3/25/16 12:26:36.000 PM kernel[0]:[90911 | 0]com.apple.CodeSi async。扫描:#'/Applications/VI.app/Contents/Info.plist'act=0x00000002 rsn='ETYP1'ln='604'
工作得很好,谢谢@craig smithNote,当所有的iTerm窗口最小化时,这会崩溃,因为没有
当前窗口
。修复了一个简单的
try
block。几年后返回,因为我再次需要它。这个答案出现在最初正确答案的几个月之后,非常棒,因为如果vim已经在iTerm中打开,它会在vim中的一个选项卡中打开文件。如果没有,则按照原始问题打开iTerm和vim。