Saturday, April 19, 2014

#1 2012-02-13 09:33:11 am

FlameCoder
Member
Registered: 2009-10-28
Posts: 44

ASOC Call to C

I'm trying to figure out how to call a C function from ASOC.

I created two files mathSum.h with code...
int sum (int x, int y);

and mathSum.c with code...
#include "mathSum.h"
int sum (int x, int y)
{
    return (x + y);
}

In my AppDelegate.applescript I have...
        property parent : class "NSObject"
    property textField : missing value
    property myCcall : class "mathSum"

    on runCcode_(sender)
        set myCall to mathSum's alloc()'s init()
        myCall's sum(10, 11)
        textField's setStringValue_(myCall's sum(10, 11) as string)
    end runCcode_

...But I get an error that mathSum is not defined... How do I make a call to C? Thanks!!!

Offline

 

#2 2012-02-13 09:37:10 am

FlameCoder
Member
Registered: 2009-10-28
Posts: 44

Re: ASOC Call to C

property myCcall : class "mathSum" should have read
property myCall : class "mathSum" ... But still get same error. Not sure how to call a C function from ASOC.

Offline

 

#3 2012-02-13 09:56:30 am

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

Re: ASOC Call to C

FlameCoder wrote:

I'm trying to figure out how to call a C function from ASOC.
...But I get an error that mathSum is not defined... How do I make a call to C? Thanks!!!

You Can't call C functions from ASOC. ASOC is only able to call selectors, which are null terminated strings pointing to an pointer function in C. To make it possible to call C functions indirectly is creating an Objective-C wrapper around it. This way you have a pointer to an function in your Objective-C run time and this function is running C code like this

//in an Objective-C class
- (int) length: (NSString *)str
{
return strlen([str utf8String]);
}

This way you have wrapped Objective-C code around C function strlen.


Kind regards

You can chat here about AppleScript

Offline

 

#4 2012-02-13 10:14:35 am

FlameCoder
Member
Registered: 2009-10-28
Posts: 44

Re: ASOC Call to C

Sorry, this is not enough information for me to understand. How do I go about calling the simple C functions I have stated above indirectly? What files do I make and how is it called from ASOC?

Thanks,

Offline

 

#5 2012-02-13 10:39:31 am

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

Re: ASOC Call to C

You have to create an objective-C function in a .m file.  So you could do something like:

MathSum.h

Applescript:

@interface MathSum : NSObject
+(NSInteger)add:(NSInteger) x to:(NSInteger) y;
@end

MathSum.m

Applescript:

#import "MathSum.h"

@implementation MathSum

+(NSInteger)add:(NSInteger) x to:(NSInteger) y {
return x+y;
}

@end

You would then call it in ASOC like:

Applescript:

log current application's MathSum's add_to_(10,11)

Ric

Offline

 

#6 2012-02-13 10:59:05 am

FlameCoder
Member
Registered: 2009-10-28
Posts: 44

Re: ASOC Call to C

I was able to make this work. Thank you... Suppose I have some files written in just C and not ObjC.... do I need to rewrite them in ObjC or is there another way to add the files to my project and be able to use them in ASOC?

Offline

 

#7 2012-02-13 11:15:52 am

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

Re: ASOC Call to C

As far as I know, you do have to redo them in objective-c, although "rewrite" might be too strong a word.  You can put whatever c code you want inside your objective-c methods as long as the method signature has all the arguments you need to get your c code to work. So, mostly it's just the name of the function that you have to create (and maybe some conversions to c types), the insides can still be all c, and shouldn't require much rewrite.

Ric

Offline

 

#8 2012-02-13 02:12:38 pm

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

Re: ASOC Call to C

FlameCoder wrote:

Sorry, this is not enough information for me to understand...

Sorry, I assumed that when you have written C code you understand how C and a superset like Objective-C works.


Kind regards

You can chat here about AppleScript

Offline

 

#9 2012-02-15 05:00:34 am

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

Re: ASOC Call to C

I'm still using 10.6 but I read somewhere that in 10.7/Lion it is possible to call a C-routine from AppleScript if you tell the current application: "tell current application to NSRectFill(aRect)". Does this work with every C-routine or only with routines in Apple's frameworks?

Offline

 

#10 2012-02-15 05:38:25 am

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

Re: ASOC Call to C

Chocoholic wrote:

I'm still using 10.6 but I read somewhere that in 10.7/Lion it is possible to call a C-routine from AppleScript if you tell the current application: "tell current application to NSRectFill(aRect)". Does this work with every C-routine or only with routines in Apple's frameworks?

Only with those in Foundation, from memory. I believe it should be all frameworks with bridge support files, provided they are loaded at launch time, but the test I did when it came out suggests this was not the case. And in practice, only a selection are usable because many require structures ASObjC can't handle. ASObjC is limited to a handful: NSRange, NSSize, NSPoint, and NSRect.

Last edited by Shane Stanley (2012-02-15 05:42:47 am)


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

Offline

 

#11 2012-02-15 06:51:20 am

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

Re: ASOC Call to C

It's not possible like I said before unless some C functions are defined as selectors (with NULL objects) in the Objective-C runtime or by the AppleScriptObjC framework. But still they're always selectors and never functions that you actually call.


Kind regards

You can chat here about AppleScript

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.035 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)