I made an applet that contains two custom handlers, handler_a and handler_b. Running their code as standard unwrapped AppleScript statements (without the handlers) from Script Editor was successful. However, sending the command tell application “My_Applet” to handler_a from another script results in the error “Connections invalid”. Why is that? All I found on the Internet is that the application may be quitting prematurely. However, shouldn’t macOS handle that in the background, without me caring about little bits and pieces? What could I do to smooth it out?
The following approach worked on my Sequoia computer:
tell application "My Script Application"
launch
helloDialog("Hello") --a handler in "My Script Application"
end tell
This is discussed in the AppleScript Language Guide in the section entitled Calling a Script Application From a Script:
This is wishful thinking.
In due course, I’ve read about AppleScript handlers thoroughly. On facing this issue I sat down to refresh my knowledge. I read, re-read and double-checked I was on the right path. I found nothing to assume I misinterpreted the patterns and implications.
By the time I started this thread, I had already crossed your suggestions out. None of launch and activate was a success.
What I need is to pass arguments to the applet. I vainly tried enclosing relevant parts of the code in on run argv...end run
instead of my custom handler. Fetching its single member (as the input is projected to be just one text item) with one-liners as simple as argv's item 1
and length of argv
raised an error telling me something to the effect that «can’t make argv’s item 1 into type string» and «can’t make length of argv into type string»; I don’t remember the exact wording.
The only observable difference was the error message. Calling the custom handler from another AppleScript returned “Connection invalid” while running open /My/Applet.app --args the_input
on the command line (on a version with on run argv..end run
and the actual string in place of the_input) returned the two alerts in the preceding paragraph.
In Finder AppleScript dictionary, I came across the command open...with properties
. Its entry stipulates that with properties
takes a record. A record of what? And, most importantly, how should I structure my code for it to accept this record? Finally, is this command the analogue of the shell’s open ... --args
?
Script applets normally quit as soon as they’ve finished running. It sounds as if your applet’s launching in response to tell application “My_Applet”, then, having no runtime code of its own, quitting immediately before getting the call from outside to its handler. Have you tried saving the applet as a stay-open application? (Tick the “Stay open after run handler” option when saving as an Application.)
Oops, waitforamin’ …
Launch actually did the trick. Both run and activate failed, even though the official AppleScript guide appended run after launch in the example of starting an applet. Weird.
More interesting is why the shell version failed too.