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

For many years, I used the following AppleScript to open txt files in vim in iTerm2, but since iTerm 2.9.2 (aka iTerm3) it broke. Can anyone advise how to update this AppleScript so that it works again?

    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

I originally copied the script from http://earthwithsun.com/questions/283418/how-can-i-make-terminal-vim-my-default-editor-application-in-mac-os-x , but I have no experience AppleScript

Any help is most appreciated! IN

+4
source share
5 answers

Using the @dusty link, I recommend that you change the entire code section of your 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, , , .

,

+6

Applescript terminal , . , , : , .

, :

  • ,
    • ,
      • ,

https://iterm2.com/applescript.html .

+2

( !) , , . , , - vim, iTerm. , , NerdTREE fzf.vim happy.

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
+2
source

Something like this in 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

Save as .app, and then open files through it - perhaps make it .app by default "Open with."

0
source

I took inspiration from previous announcements and made several extensions. My solution accepts multiple input files (e.g. from selection). If there is an active window, the script searches for open vim sessions on any tab and opens the files inside this vim instance. If not, a new tab or iTerm window is created.

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
0
source

All Articles