ASOC called from Objective C

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 was reading this but it was no help honestly: http://macscripter.net/viewtopic.php?id=30478

Thanks, Chris

Hi,

I’m doing it this way: for example I have an ASOC script class ASSystemEvents.applescript

in the Cocoa class I declare an instance variable

id systemEvents;

and define it in awakeFromNib

systemEvents  = NSClassFromString(@"ASSystemEvents");

For the methods declaration I’m using a header file as category of NSObject named +ASOC.h

@interface NSObject (ASSystemEvents)
- (NSString *)someASOCderivedString:(id )sender;
@end

and import it in the Cocoa class


#import "<application name>+ASOC.h"

That’s it

An example I posted here

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):

@interface AppDelegate <NSObject>
-(void)loadManual:(id)sender;
@end

In your .m file call it like this:

[appDel loadManual:self];

Ric

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):

@interface NSObject (AppDelegate) 
-(void)loadManual:(id)sender;
@end

Thanks guys! I used Stefan’s approach but I think DJ’s would work also. That wasn’t something I would have figured out myself easily.

I use something like:

@interface ASClassName <NSObject>

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

interface, in a header file or somewhere else:

interface myASOCclass : NSWindowController {

}

- (NSString *)someASOCderivedString;

@end

use myASOCclass in Objective-C:

myASOCclass *myWindowController = [[NSClassFromString(@"myASOCclass") alloc] initWithWindowNibName:@"MyNib"];
[myDocument addWindowController:myWindowController];
NSString *aString = [myWindowController someASOCderivedString];

Can I tell Xcode that .h is a counterpart of .applescript?

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.