[Pythonmac-SIG] embed python in cocoa

Jerry Isdale isdale at avinc.com
Tue May 20 02:15:46 CEST 2008


Georg wrote:
> Hello,
>
> I want wo be able to make my program scriptable with python.
>
> My program is written in ObjectC in XCode 3 (target only for MacOSX
> 10.5).
>
> I want to embed it and use
>
> Py_Initialize();
> PyRun_SimpleString(".....
>
> Can anyone give me some advice how I wrap my cocoa classes to be able
> to access them from within the python script?
>
> I found the examples on how to wrap c/c++ functions but nothing about
> ObjectC.
>
> Many thanks in advance
> Georg



I've started working on such a thing too and am a bit lost by the lack  
of documentation. I tried a more ambitious route first but then moved  
back to a simple case.

Suppose I have a simple Obj-C class in the files MyClass.h/m:

@interface MyClass : NSObject {
     float lat;
     float lon;
     float alt;
}

@property float lat;
@property float lon;
@property float alt;

@end

@implementation MyClass

@synthesize lat;
@synthesize lon;
@synthesize alt;

- (NSString *)description
{
     return [NSString stringWithFormat:@"myClass lat(%f) lon(%f)  
alt(%f)", lat, lon, alt];
}
@end

Now I want a simple command line Obj-C tool that runs a python script  
that creates a MyClass and prints the description.  I used XCode 3.0  
on my Leopard box to create a Cocoa-Python application.  I dont want  
all the window stuff, so I changed the main.py to simply:

import objc
from Foundation import *
NSLog("Hello World! Running main.py.")

and that works.
Then I changed it to be...
myInst = MyClass.alloc().init().description()
NSLog("myInst is: %@",myInst.description())

and *that* worked.... note that I did Not import or otherwise bring in  
MyClass
Trying this with my real classes off in a library also worked.

so far that looks good and enough for today's effort.

Next I'd like to write python scripts that would animate the values of  
MyClass - eg set lat/lon/alt in a loop that uses some sort of function.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythonmac-sig/attachments/20080519/869b6617/attachment-0001.htm>


More information about the Pythonmac-SIG mailing list