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.

Previously:
The editing pass
October 9, 2007

Some thoughts about making an explicit editing pass on working code:

Once you get a piece of code to the point where you believe it works - it’s passing its tests - go back over it and edit it. That is, go back and edit it for clarity, flow, and style. Just as if it were an essay.

read the rest.
NSLocalizedString can set errno
July 10, 2007

This is a short one, but it might help someone with debugging someday.

A while back, we had some code that was checking errno, then using the NSLocalizedString macro to get a localized error message, but it checked errno again later. Only the app isn’t localized yet. There’s no Localizable.strings file, so when NSBundle -localizedStringForKey gets [...]

read the rest.
Mac programming collaborative bookmarks?
February 7, 2007

I usually like the link selection I get from the Joel Reddit, which usually has good software-related essays at the top. It seems to avoid links to uninformed rants about consumer electronics or industry politics, for which I have no use.

I’d like a social links site for software professionals on the Mac - does one [...]

read the rest.
Next Page »
Feed, Endorsements & other Links

my bookmarks

© 2005 - 2007 Michael McCracken.