You are not logged in.
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
Offline
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
Applescript:
id systemEvents;
and define it in awakeFromNib
Applescript:
systemEvents = NSClassFromString(@"ASSystemEvents");
For the methods declaration I'm using a header file as category of NSObject named <application name>+ASOC.h
Applescript:
@interface NSObject (ASSystemEvents)
- (NSString *)someASOCderivedString:(id )sender;
@end
and import it in the Cocoa class
Applescript:
#import "<application name>+ASOC.h"
That's it
Last edited by StefanK (2012-02-07 11:26:03 am)
Offline
An example I posted here
Last edited by DJ Bazzie Wazzie (2012-02-07 11:38:15 am)
Offline
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):
Applescript:
@interface AppDelegate <NSObject>
-(void)loadManual:(id)sender;
@end
In your .m file call it like this:
Applescript:
[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):
Applescript:
@interface NSObject (AppDelegate)
-(void)loadManual:(id)sender;
@end
Last edited by rdelmar (2012-02-07 12:20:46 pm)
Offline
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.
Last edited by SuperMacGuy (2012-02-07 12:23:55 pm)
Offline
I use something like:
Applescript:
@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...
Offline
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:
Applescript:
script myASOCclass
property parent : class "NSWindowController"
on someASOCderivedString_()
return "blah blah blah"
end
end script
interface, in a header file or somewhere else:
Applescript:
interface myASOCclass : NSWindowController {
}
- (NSString *)someASOCderivedString;
@end
use myASOCclass in Objective-C:
Applescript:
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?
Offline
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:
#import <Cocoa/Cocoa.h>
@protocol ScriptMethodsProtocol
//this is a protocol to declare methods for ScriptMethods
//so no ivars only methods
-(NSArray *)currentDocTCNum:(id)sender;
-(void)placeImgInDoc:(NSString *)imageURL codeDestination:(NSString *)codeLoc;
@end
In my Objective-C's app delegate method:
#import "ScriptMethodsProtocol.h"
...
@interface ApplicationDelegateClassName : NSObject <NSApplicationDelegate>
{
NSWindow *window;
IBOutlet id asocScripts;
}
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.
Offline