[Pythonmac-SIG] Python control/integration of a Cocoa/Quicktime application?

Dethe Elza delza at livingcode.org
Sat Oct 27 08:18:05 CEST 2007


If you write an Objective-C framework, the python code to wrap it
using PyObjC is very short.  Here is an example I use to expose Tim
Omernick's CocoaSequenceGrabber framework to capture images from the
iSight camera:

<example>

import objc, AppKit, Foundation, os
if 'site-packages.zip' in __file__:
  base_path = os.path.join(os.path.dirname(os.getcwd()), 'Frameworks')
else:
  base_path = '/Library/Frameworks'
bundle_path = os.path.abspath(os.path.join(base_path, 'CocoaSequenceGrabber.fram
ework'))
objc.loadBundle('CocoaSequenceGrabber', globals(), bundle_path=bundle_path)
del objc, AppKit, Foundation, os, base_path, bundle_path

</example>

I have that saved as PySight/__init__.py so I can import * from
PySight to get all the objects and methods from the framework.  It
would be shorter, but I have some path manipulation so that it works
from the command line and from within an application bundle built with
py2app.  The bare minimum you need is:

import objc
objc.loadBundle('MyBundle', globals(),
bundle_path='/my/bundle/path/MyBundle.framework')

Writing a bundle in Python that can be imported by an Objective-C
application is similarly easy.  I have some blog posts on that topic
if you ever decide to try that direction.  The application just needs
to take Objective-C bundles as plugins, it does not have to plan for,
or even know about, Python in the bundle implementation.

HTH

--Dethe


More information about the Pythonmac-SIG mailing list