Maybe I am getting dumb before lunchtime here. But how do I call ASOC methods from Cocoa? I totally understand calling Cocoa from ASOC, or from having a button simply activate my ASOC script’s methods. But how do I declare the class or instance and then call an ASOC method in ObjC code?
do I: #import “myASOCclass.applescript”? Tried that and it totally didn’t work compiling, a million errors
do I: IBOutlet id myASOCobject, and then link in IB? Sort of tried that without much luck.
After I get things setup/importing, then to call it is like how? Like this?:
NSString *aString = [ASOC someASOCderivedString:nil];
where the ASOC code is something like:
on someASOCderivedString_(sender)
return “blah blah blah”
end
I think the easiest way was mentioned by Shane in the same thread as DJ posted above:
In your .h file declare an outlet to your applescript file like: IBOutlet id appDel;
Hook up the IBOutlet in IB.
At the top of your .m file (above the @implementation line) put in an informal protocol declaration and a method signature for any methods in the applescript class that you want to use (where AppDelegate is the name of my applescript in this example):
After edit: Actually, I’m not sure of the right nomenclature here or the right way to declare an informal protocol – maybe Shane can explain his example better. It seems like I’ve also seen it this way (they both work in my case):
but it’s pretty flexible. The class name is there for my benefit, for when I have more than one AS class.
The other form is for creating a category rather than an informal protocol.
If you use an informal protocol, you don’t have to worry about the #import statement. But they’re both about patting the compiler on the head and telling it to calm down…
The compiler wants to check everything and needs some information to do it. Give it the information and it is happy.
I’m still trying to understand how ASOC works, can I do this:
script myASOCclass
property parent : class "NSWindowController"
on someASOCderivedString_()
return "blah blah blah"
end
end script
I noticed that I was calling only class methods to my ASOC object. Even though I declared them as instance methods.
What I ended up doing was (just for reference of others in the future):
Make a protocol file, call it “ScriptMethodsProtocol.h”
contents are:
In my Objective-C’s app delegate method:
Then, in Interface Builder, I dragged a Blue Cube into the project, and then made it’s class be “ScriptMethods” (which is my ASOC file, ScriptMethods.applescript)
Then drag a line from the delegate’s list of IBOutlets (asocScripts) to the ScriptMethods blue cube. So it’s connecting an instance of the script that is created on application start up.