- Open Automator and create a new application. You can name it "TerminalVim"
- Works with any command basically, just change the following line
set q to "nvim " & quote & myPath & quote
to match what you want in the code below. For example:set q to "vim " & quote & myPath & quote
to use Vim instead of NeoVim. - Paste the following code:
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
on run {input, parameters}
set myPath to POSIX path of input
set q to "nvim " & quote & myPath & quote
if appIsRunning("iTerm") or appIsRunning("iTerm2") then
run script "
on run {q}
tell application \":Applications:iTerm.app\"
activate
try
select first window
set onlywindow to false
on error
create window with default profile
select first window
set onlywindow to true
end try
tell the first window
if onlywindow is false then
create tab with default profile
end if
tell current session to write text q
end tell
end tell
end run
" with parameters {q}
else
run script "
on run {q}
tell application \":Applications:iTerm.app\"
activate
try
select first window
on error
create window with default profile
select first window
end try
tell the first window
tell current session to write text q
end tell
end tell
end run
" with parameters {q}
end if
end run
- Save this App under /Applications.
- Find a file, right-click, "Get Info" and under "Open With" click "Other..." change its default application to be our new app. If you can't see the app in the list provided, then change next to "Enable" from "Recommended Applications" to "All Applications"
- Optionally click "Change All..." to make it default for every file.
Follow previous steps but with the following script:
on run {input}
set the_path to POSIX path of input
set cmd to "vim " & quoted form of the_path
tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
tell application "Terminal"
activate
if terminalIsRunning then
tell application "System Events" to keystroke "t" using {command down}
delay 0.2
do script with command cmd in window 1
else
do script with command cmd in window 1
end if
end tell
end run
I created a alfred script below, based on the @rpapallas code
I trigger this code with a F2 key on finder (select a file that i want to open on neovim and press f2, only works on finder)
