External script (loaded) calling a delegate method

If a delegate loads an external script file (not part of the project), what is the best way to have that external script call a method in the parent who loaded it? I’m gettin this to work now although in a very convoluted way which happens to also be crashing in a stress test.

Secondarily, is it possible for the external script to access (read or write) to the parent’s properties directly?

I figured out a better way to do it so my question might be self-answered. This is what I’m doing in case there is a better way:

In the AppDelegate and the external script I have this global:

global gAppDelegate

In applicationWillFinishLaunching, I added this:

 set gAppDelegate to a reference to class "AppDelegate"

The external script can now call functions or set properties of the AppDelegate (parent) that loaded it

gAppDelegate's test_input("Hello World")

set gAppDelegate's fieldContents to "Howdy"

It works great in my isolated test project. Will have to wiggle it into my real app and see how things go.

If something works, that’s the ultimate test, but I’d be a bit wary simply because you’re referring to the class itself, rather than the particular instance running in your app.

AppleScript doesn’t accurately reflect the class/instance division very well, but it’s worth keeping in mind that in an Xcode project your script is effectively turned into an instance of a class called AppDelegate (or whatever name you use).

Try setting your global to current application’s NSApp’s delegate() instead. Although mixing AppleScript globals and ASObjC pseudo-classes always makes me feel a little nervous.

Where are you loading these files from, if I may ask? Are they ASObjC code?

They are script editor files, AppleScript code.

And, weirdly, after patching the technique into my project and running it every minute for 3-4 hours successfully, I quit and tweak an interface element (completely unrelated to this) and now I am getting an error that the subroutine I"m calling isn’t found in the AppDelegate.

Tried rebuilding, clean rebuild, computer restart, etc.

The technique works great in my proof of concept still but in the main app after perfect success all day, now it just won’t work.

Having changed the global based on your last suggestion, it now “sees” the subroutine but is suddenly complaining about naming with this:

expected 0 arguments, got 2

So I am fully confused how I got this to work so well earlier.

When you’re doing this, you have to keep in mind that you’re effectively calling a Cocoa method on an instance of the AppDelegate class, not a standard AppleScript handler. So normal Cocoa method naming rules apply, specifically with underscores. And the results are likely to be returned as Cocoa objects, and will need coercing to their AS equivalents.

Now working and will stress test it tonight. Thanks again for helping to clarify some of the objC voodoo.