ASOC: calling AppleScript->ObjC->AppleScript?

I want to write an ASOC application which starts running within the AppleScript environment and calls an ObjC method which calls an AppleScript handler within the same AppleScript environment from which the application started.

I know how to call from AppleScript into cocoa, but I don’t know how to make the call from cocoa back to the AppleScript handler.

Suppose my AppleScript app delegate looks like this:

script MyAppDelegate

property parent : NSObject
property theProperty : "didn't run yet"

on applicationWillFinishLaunching_(notification)
    set theProperty to "it is running now"
    doit() of class "Util" of current application
end applicationWillFinishLaunching_

on theHandler_(item)
    display dialog (theProperty & " ... " & item)
end theHandler_

end script

Suppose my Objective C code looks like this. First, the Util.m file:

… and now, the Util.h file:

How do I implement the doit method within Util.m so that when I run this over-simplified sample application, a dialog box will pop up with the following text?

it is running now … foobar

Thanks in advance.

The answer depends a bit on whether you are just using class methods, or have instances. But something like this should get you started:

[NSClassFromString(@“MyAppDelegate”) theHandler:@“foobar”];

or:

[[NSApp delegate] theHandler:@“foobar”];

Thank you.

In the example I gave above, I’m guessing that I’m calling an instance method in my delegate, correct?

Yes – instance method of the delegate, otherwise a class method. AS handlers can be called as either.

Thank you very much.