Launch terminal program from macOS app

command lineitermterminal

I use Fork and I'm trying to set up a custom command that will open a file in Vim. I'm not using MacVim, so the command needs to open a terminal, preferably iTerm2.

Executing /usr/local/bin/vim doesn't work because there's no terminal, so my command needs to launch a terminal and run Vim inside it. I think the open command should work:

/usr/bin/open /usr/local/bin/vim --args <absolute_file_path>

This opens a new iTerm2 tab and Vim launches inside it, but the file isn't opened. :ls shows that I'm editing a new buffer. If I pass the -h flag instead of a filename, Vim opens normally, so it's definitely not receiving the argument (or else it would display the help text instead of opening).

I get the same result even if I run the open command at the terminal, so I don't think this is Fork's issue.

Is there a command I can execute that will launch a terminal and run Vim inside it, passing an argument to Vim?

enter image description here

Best Answer

This only works with iTerm2, but I ended up using the following script I modified from one Stack Overflow that opens a new tab:

#!/usr/bin/env bash

osascript <<EOF
    tell application "iTerm"
        activate
        set win to create window with default profile
        tell current session of win
            select
            write text "vim '$1' && exit"
        end tell
    end tell
EOF

This opens a new iTerm window, runs vim inside it, then exits the shell when vim exits.