Wednesday, July 23, 2014

#1 2012-02-07 11:03:36 am

SuperMacGuy
Member
From: Amish Country, Lancaster, PA
Registered: 2004-06-23
Posts: 416
Website

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


Scripts for InDesign: http://chris.paveglio.com
ASOC & Cocoa, specializing in InDesign, Photoshop, Ai, FileMaker

Offline

 

#2 2012-02-07 11:16:07 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10454
Website

Re: ASOC called from Objective C

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)


regards

Stefan

Offline

 

#3 2012-02-07 11:38:04 am

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 1850

Re: ASOC called from Objective C

An example I posted here

Last edited by DJ Bazzie Wazzie (2012-02-07 11:38:15 am)


Kind regards

Offline

 

#4 2012-02-07 12:00:54 pm

rdelmar
Member
From: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: ASOC called from Objective C

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

 

#5 2012-02-07 12:23:31 pm

SuperMacGuy
Member
From: Amish Country, Lancaster, PA
Registered: 2004-06-23
Posts: 416
Website

Re: ASOC called from Objective C

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)


Scripts for InDesign: http://chris.paveglio.com
ASOC & Cocoa, specializing in InDesign, Photoshop, Ai, FileMaker

Offline

 

#6 2012-02-07 04:38:39 pm

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

Re: ASOC called from Objective C

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...


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

Online

 

#7 2012-02-09 06:07:14 am

Chocoholic
Member
Registered: 2012-01-30
Posts: 21

Re: ASOC called from Objective C

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

 

#8 2012-02-09 02:24:00 pm

SuperMacGuy
Member
From: Amish Country, Lancaster, PA
Registered: 2004-06-23
Posts: 416
Website

Re: ASOC called from Objective C

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.


Scripts for InDesign: http://chris.paveglio.com
ASOC & Cocoa, specializing in InDesign, Photoshop, Ai, FileMaker

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.025 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)