michael-mccracken.net

PyObjC notes

I’ve been hacking around with PyObjC, the Python-ObjC/Cocoa bridge recently, and it’s quickly becoming my favorite way to write Cocoa apps. It’s really natural to mix Python idioms with Cocoa objects.

The latest version of PyObjC is 2.0, it’s installed by default on OS X 10.5, and XCode now includes templates for starting a PyObjC project. There’s even code autocomplete in XCode for PyObjC and IB integration, so aside from some smart-indenting issues, writing PyObjC in XCode is almost as natural as writing in ObjC.

I thought I’d post a few nice shortcuts and tips here.


You can use tuples for NSRect/Range/Point, for instance, this -

r = NSInsetRect(((0, 0) ,
                 (100, 100)), 
                10, 10)

creates this NSRect -

NSRect origin=<NSPoint x=10.0 
                       y=10.0> 
       size=<NSSize width=80.0 
                   height=80.0>>


Passing python arrays as NSArray instances (and dictionaries as NSDictionaries) works great, but sometimes you need to pass a C array. The Python ‘array’ module handles that nicely:

import array
g = NSGradient.alloc().
   initWithColors_locations_colorSpace_(
    [NSColor.whiteColor(), 
     NSColor.blackColor()], <br/>
    array.array('f', [0.0, 1.0]), 
    NSColorSpace.deviceRGBColorSpace())


ObjC selectors are just python strings in PyObjC.

defNC.addObserver_selector_name_object_(self,
  'windowDidResize:',
  NSWindowDidResizeNotification,
  self)
# or
self.performSelectorOnMainThread_withObject_waitUntilDone_('doIt:', None, False)
# or 
if o.respondsToSelector_("fun:"): return o.fun_(a)


Finally, something that comes in handy when working with KVC, the ‘_’ method now defined on NSObjects in PyObjC:

o = <Some ObjC object>
print o._.myKey
o._.myKey = 44
# is equivalent to:
print o.valueForKey_('myKey')
o.setValue_forKey_(44, 'myKey')


That last example is straight from the NEWS page, where lots of other useful info can be found.

Comments:

Leave a reply

You must be logged in to post a comment.

Feed, Endorsements & other Links

my bookmarks

© 2005 - 2007 Michael McCracken.

buy cheap levitra cheapest clomid prices viagra sales propecia sale buy cheapest cialis viagra prescription viagra malaysia order clomid online soma pharmacy levitra sale certified cialis buy cheap cialis internet buy lasix without prescription synthroid discount buy viagra online cheap order viagra no prescription required viagra side effects buy viagra buy viagra overnight delivery viagra free delivery buy cialis overnight delivery cost cialis order synthroid online soma online cheap viagra from canada buy viagra in canada soma prescription order cialis from canada order discount cialis online accutane sale viagra overnight delivery generic viagra online purchase cialis without prescription lasix sale cialis cheapest synthroid cialis cheapest price order viagra overnight delivery low cost cialis order no rx cialis cheap viagra from usa clomid find discount viagra accutane purchase acomplia propecia pills viagra cheap soma cheap lasix propecia prices buy cialis internet buy generic synthroid purchase soma online lowest price accutane cialis in australia generic cialis propecia no prescription clomid prescription buy cheapest viagra on line buy synthroid discount viagra no rx cheapest acomplia prices generic soma order viagra no rx buy propecia buy viagra generic cheap cialis tablets discount cialis without prescription order levitra online acomplia without a prescription viagra purchase acomplia online cheap