As already mentioned, the error is from targeting iTerm’s current window
, since there isn’t one (you closed it). I don’t have iTerm, but the solution for that would be to check if it has any windows, and if not, make or open one. Also note that Automator’s input
is a list.
If you use Terminal’s do script
command, it will open in a new window (unless otherwise specified) so you don’t have to deal with that. If all you are using Automator for is the script and arguments, a regular AppleScript application can be used, which is a little more flexible. Since applications can be command-dragged to the Finder window’s toolbar, you can also skip setting file types to open with the app, it can get the current selection.
The following is a regular AppleScript droplet that files can be dropped onto, it can be double-clicked to just ask for file items, and clicking the toolbar icon (if placed in the Finder window) will pass the current selection to the app. It can even be called from the command line, all of which will open a new Terminal window running Helix (or Neovim). Save the script as an application; I am assuming that you have Helix (or Neovim) configured.
use framework "Foundation"
use scripting additions
on run args
try
set selectedFiles to {}
if args is in {current application} then -- app double-clicked or 'open -a' with --args option
set processList to (current application's NSProcessInfo's processInfo's arguments) as list
if (count processList) > 0 then set selectedFiles to rest of processList -- first item is the executable
else if args is not in {"", {}, missing value} then -- osascript with arguments
set selectedFiles to args
end if
if selectedFiles is {} then
tell application "Finder" to if (get windows) is not {} then -- try current Finder selection
set selectedFiles to get selection as alias list
end if
if selectedFiles is {} or (((path to me) is in selectedFiles) and ((count selectedFiles) is 1)) then -- choose dialog
activate me
set selectedFiles to (choose file of type "public.text" with multiple selections allowed)
end if
end if
open selectedFiles
on error errmess
display alert "Error in 'run' handler" message errmess
end try
end run
on open droppedFiles -- items dropped onto the app
doStuff(droppedFiles)
end open
to doStuff(theFiles) -- main
set arguments to ""
repeat with anItem in theFiles -- a list of files will load in a common window
try
if (path to me) is not in {contents of anItem, POSIX path of anItem} then -- skip the app if path is in the list
set anItem to quoted form of POSIX path of anItem
set arguments to arguments & anItem & space -- build argument string
end if
on error errmess
display alert "Error in 'doStuff' handler" message errmess
end try
end repeat
tell application "Terminal"
activate
do script "hx " & arguments
end tell
end doStuff