Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

For years I've been using the following AppleScript to open txt files in vim in iTerm2, but since iTerm 2.9.2 (aka iTerm3) it's broken. Could anyone advise how to update this AppleScript so 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 AppleScript experience whatsoever.

Any help most appreciated! B

like image 959
B-30 Avatar asked Feb 27 '16 05:02

B-30


People also ask

How to open a file in Vim?

Vanilla Vim 1 Opening files from the Command line. ... 2 Switching files after opening (Buffers) Vim will open all three files in ‘buffers’ but you might not see them (see this article for help setting up a plugin that makes ... 3 Open files with :edit command. ... 4 Netrw the built in file browser. ...

How do I autocomplete a file in Vim?

Vim has an auto completion engine called “wildmenu” (activate it with :set wildmenu or add set wildmenu to your .vimrc). With the wildmenu setting active while typing a file path press <Tab> and vim will autocomplete the path.

How to set default file types to open with Neovim?

To do this we need to use Finder to find a file of each type you want to default to open with Neovim in iTerm. Then we will change the association in MacOS for that file that might already exist to point to our new application.


1 Answers

Using @dusty's link, I recommend that you change the entire code section of your Run AppleScript Action to this:

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

It seems to work on my machine, but I never use vim, so I am not sure if this will give you precisely what you want.

Good luck,

like image 155
Craig Smith Avatar answered Nov 15 '22 08:11

Craig Smith