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

时间:2016-02-27 05:43:05

标签: vim applescript iterm2

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

最值得赞赏的任何帮助! 乙

5 个答案:

答案 0 :(得分:6)

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

ILGenerator.Emit(OpCodes.Ldc_I8, ptr)

它似乎可以在我的机器上运行,但我从不使用vim,所以我不确定这是否能为您提供您想要的产品。

祝你好运,

答案 1 :(得分:2)

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

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

  • 包含一个或多个的应用程序
    • 包含一个或多个的窗口
      • 包含a的标签
        • 会话
已使用新语法的示例更新了

https://iterm2.com/applescript.html

答案 2 :(得分: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

答案 3 :(得分: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成为默认值"打开"应用

答案 4 :(得分:0)

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