Calling another Applescript's handler from external Applescript

This is a representation of my primary Applescript MyScript1:

property gVar1 : ""
property gVar2 : 0

on run
   display dialog "Ask a question" buttons {"Cancel","OK"} default button "OK"
   if button returned of result = "OK" then
      gHandle1()
   else
      return
   end if
end run

on gHandle2(LocalVar1)
   if LocalVar1 = "widget1" then return gVar1
   if LocalVar1 = "widget2" then
      gHandle1()
      return gVar2
   end if
end gHandle2

on gHandle1()
   gVar2 = gVar2 + 5
end gHandle1

My secondary calling script, named MyScript2, would be something like:

tell application "MyScript1" to gHandle2("widget2")

My purpose of all this is to get property data out of MyScript1 without incurring a display dialog box.
MyScript1 is an Applescript compiled application. If I double-click on the MyScript1 application, I want the display dialog to ask me what to do. If MyScript2 activates MyScript1, I do NOT want the display dialog box to appear. (It does because the run handler is running on launch; I thought if you called another handler, “on run” didn’t execute like when the “on open” handler executes. Guess I’m wrong!)

Also, MyScript2 gets an invalid connection error when trying to call MyScript1’s gHandle2 handler.

I could have sworn that you could call one Applescript’s handler from another script… It’s been a long time since I’ve written Applescript code. So, can someone tell me what I’m doing wrong or tell me how better to perform what I want?

Thank you,
Rob

Hi.

If you treat the first script as an application, it’ll run (displaying the dialog) and then quit when it’s finished (giving an invalid connection with the calling script).

You either have to save the first script as a stay-open application, so that, once it’s running, it’s always open to have its handlers called by other scripts, or, probably better in your case, load it into the calling script something like this:

set script1 to (load script file "path:to:MyScript1.app") -- Obviously use the correct path here.
tell script1 to gHandle2("widget2")

Hello.

Possessive form also works well, like:


set a to myScripts''s valueGenerator()

and properties like this:


set propCopy to theProperty of myScript

This is fairly well documented in the AppleScript Language Guide. It is well written with examples, look for “Objects”.

And even if someone doesn’t like it, when you load script, you also get access to global variables directly, without and further dereferencing, for both reading and writing. :slight_smile: