Browse files using Finder but open text files using command-line Vim

I prefer to browse files using Finder, but to open text files using command-line Neovim (well, in fact I prefer Helix…).

I found the following Automator script and it kinda works but not faultlessly.

on run {input, parameters}
  set filename to quoted form of POSIX path of input
  set cmd to "clear;cd `dirname " & filename & "`;nvim " & filename
  tell application "iTerm"
    tell the current window
      create tab with default profile
      tell the current session
        write text cmd
      end tell
    end tell
  end tell
  
  tell application "System Events"
    tell process "iTerm2"
      set frontmost to true
    end tell
  end tell
end run
  1. Open Automator
  2. Select to create an application
  3. Locate “Run AppleScript” and double-click it
  4. Paste this script, save it to /Applications
  5. Select a file type of your choice, e.g. txt one, press Command-I, change “Open with” to your application, click “Change All”
  6. Double-click any .txt file in Finder. It will be opened in iTerm2

Apart from I would prefer to use native Terminal instead of iTerm2, there is a bug:

Steps to reproduce:

  1. Open iTerm2
  2. Close the window, but don’t quit the app itself
  3. Double-click a .txt file. You will get the following error:
    a3b1f4a4b5022ba463e85f44961ee05ca582a2cd

How it can be fixed?