Big picture, I am trying to create an applescript that allows me to open files in neovim from Finder. Though I have found several resources for doing this, none work exactly as-is, so I have to do some modifying of my own to make it work. This is my first time messing with applescript, so forgive me if this is an easy fix. The code is as follows:
on run {input, parameters}
# Extract filenames and paths from the input
set filenames to ""
set filepaths to ""
if input is not {} then
repeat with currentFile in input
set filepaths to filepaths & quoted form of POSIX path of currentFile & " "
set filenames to filenames & name of (info for quoted form of POSIX path of currentFile) & " "
end repeat
end if
# Get the dirname of the input files
set parentDir to do shell script "dirname " & quoted form of filepaths
# seem to need the full path at least in some cases
# -p opens files in separate tabs
set nvimCommand to "/usr/local/bin/nvim -p " & filenames
# final command to send to iTerm
set finalCommand to "cd " & parentDir & "; " & nvimCommand
# Create an iTerm instance and write the nvim command
tell application "iTerm"
set newWindow to (create window with default profile)
tell current session of newWindow
write text finalCommand
end tell
end tell
end run
The problem is two-fold:
- When I give the applescript a file to open (i.e. by clicking on it in finder or using "Get Specified Finder Items" in Automator), I get this error even though I know for a fact that the file exists and is in
/Users/cremocal. - When I run the script without a given file, it correctly opens a new nvim environment in iTerm (in
$HOME), however it also opens another terminal in$HOME. If I then run the script again (with the nvim and terminal environments still open), only a nvim environment is created. I cannot for the life of me figure out why there is the second window.
What gives? Why is there a second window? Why can't the applescript find the file even when it exists? Any help would be greatly appreciated.