B-30 B-30 - 1 month ago 12
AppleScript Question

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

Answer

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,