Thursday, October 17, 2019

#1 2019-09-15 10:34:10 am

markconwaymunro
Member
Registered: 2017-11-28
Posts: 41
Website

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?

Offline

 

#2 2019-09-16 08:19:37 am

markconwaymunro
Member
Registered: 2017-11-28
Posts: 41
Website

Re: External script (loaded) calling a delegate method

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:

Applescript:

global gAppDelegate

In applicationWillFinishLaunching, I added this:

Applescript:

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

Applescript:

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.

Offline

 

#3 2019-09-16 05:43:02 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5959

Re: External script (loaded) calling a delegate method

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?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2019-09-16 09:35:39 pm

markconwaymunro
Member
Registered: 2017-11-28
Posts: 41
Website

Re: External script (loaded) calling a delegate method

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.

Offline

 

#5 2019-09-16 09:41:48 pm

markconwaymunro
Member
Registered: 2017-11-28
Posts: 41
Website

Re: External script (loaded) calling a delegate method

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

Applescript:

expected 0 arguments, got 2

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

Offline

 

#6 2019-09-16 11:05:33 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5959

Re: External script (loaded) calling a delegate method

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#7 2019-09-17 07:10:16 am

markconwaymunro
Member
Registered: 2017-11-28
Posts: 41
Website

Re: External script (loaded) calling a delegate method

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)