0

I'd like to be able to have an option to open a file inside NeoVim in the "Open With" list. That is to open a new terminal window (ITerm in my case), and execute a command that launches NeoVim on the file that I selected.

I'd like to have the option display here

The problem is that I'm unable to get past the last part of getting the file path inside the shell script.

I created a new .app file and put it inside the /Applications folder. The .app structure is very typical with the executable being a shell script:

#!/bin/sh

osascript <<EOF
tell application "iTerm"
    activate
    set new_window to (create window with default profile)
    set cSession to current session of new_window
    tell new_window
        tell cSession
            delay 0.1
            write text "\"echo it's working\""
            delay 2
            repeat
                delay 0.1
                --          display dialog cSession is at shell prompt
                set isdone to is at shell prompt
                if isdone then exit repeat
            end repeat
        end tell
    end tell
end tell
EOF

The script above launches the terminal and executes the command as expected: The result of the script execution

But at this point I'm not sure how to get the path of the selected file. I tried out using the command line arguments such as $1, $2 etc but to no success. I tried running declare -p inside the script to show all variables and their values but the file path I'm looking for is nowhere to be found. I'm not really sure what else I can try and I couldn't find the answer online (or at least I couldn't find the right search terms). Perhaps there is a better way of doing what I want to do. If that's the case then please tell me about it (Also I'm aware that NeoVim Qt exists but it is not a solution that I'm looking for. I'd like to have the behaviour that I described earlier).

Update: I managed to make it work the way I wanted by ditching the shell script and using apple's Script Editor instead. This is the AppleScript code that I used:

on run -- app double-clicked or script run from editor
    open (choose file with multiple selections allowed)
end run

on open theItems -- droplet
    -- Form a string with all the files in quotation marks
    set pathList to {}
    repeat with itemPath in theItems
        set end of pathList to quoted form of POSIX path of itemPath
    end repeat
    
    set AppleScript's text item delimiters to space
    set pathString to pathList as text
    set AppleScript's text item delimiters to ""
    
    doStuff(pathString)
end open

on doStuff(pathString) -- main handler to do stuff
    tell application "iTerm"
        activate
        set new_window to (create window with default profile)
        set cSession to current session of new_window
        tell new_window
            tell cSession
                delay 0.1
                write text "nvim " & pathString
            end tell
        end tell
    end tell
end doStuff
  • Seems like a very roundabout way of opening a file. How are you choosing the file that you want to open? – Mockman Jun 11 '23 at 17:34
  • How are you setting up and opening your .app bundle? Unless there is more to the shell script, an AppleScript application will do all that. – red_menace Jun 11 '23 at 18:04
  • @Mockman I'm right-clicking on the file in Finder, selecting the "Open With" option and then selecting my custom application with the shell script inside it. I would assume that my selection inside Finder is the way I choose it. – Yaroslav Biloshytskyi Jun 12 '23 at 18:56
  • @red_menace the .app bundle setup is very standart. I have an `Info.plist` file that points to the executable which is the shell script in my case. The way I open it is described in the comment above. I've posted the whole shell script I'm using in my question, there's nothing else going on behind the scenes. I assume that the information about the selected files isn't being passed directly to the AppleScript part since the shell script is acting as a proxy. Even then I'd still have to insert them into the `write text ""` line to open the files inside the terminal. So it's a bit more complex. – Yaroslav Biloshytskyi Jun 12 '23 at 19:06
  • Escaping shell variables in a heredoc is a bit of a pain. Is there a particular reason for using a shell script to call osascript to run an AppleScript instead of just using an AppleScript application? – red_menace Jun 12 '23 at 19:52
  • @red_menace not really, I learned about AppleScript while I was figuring out how to do it from a shell script so I haven't really researched it well. Though in hindsight it makes more sense to just ditch the shell script entirely. Would AppleScript have the variables that I'm looking for? – Yaroslav Biloshytskyi Jun 12 '23 at 20:14
  • It might be easier then to use Automator to create a service that opens the Finder selection in a given application. – Mockman Jun 12 '23 at 22:25

1 Answers1

1

Instead of using a shell script to call osascript to run an AppleScript, an AppleScript application (applet) can be created directly from the Script Editor. A droplet can be created by adding an open handler, which will be passed the items dropped onto the application or the file arguments used with the open command from the command line.

on run -- app double-clicked or script run from editor
   open (choose file with multiple selections allowed)
end run

on open theItems -- droplet
   repeat with anItem in theItems
      doStuff(anItem) -- do something with individual file item
   end repeat
end open

on doStuff(fileItem) -- main handler to do stuff
   display dialog (POSIX path of fileItem) -- or whatever
end doStuff
red_menace
  • 3,162
  • 2
  • 10
  • 18