Updates from February, 2008

  • VoodooPad 3.5

    mike 4:00 pm on February 11, 2008 | 1 Permalink

    From Flying Meat, VoodooPad 3.5 is out. (release notes here).

    It’s a solid update, including welcome image-editor integration and more. I thought I’d point out my favorite feature in the release notes:

    “You can now edit pages and sort the results in the search window (woo!).”

    I like this because it matches a kind of manual refactoring habit I picked up with Project Builder / XCode’s excellent “Find in Project…” window – I search for a keyword that marks places I need to look at and edit the files right in the search window. (Note: ‘occur-mode’ and kin are a powerful way to do the same in Emacs…)

    It’s a powerful habit if you plan for it, by using comments in code to keep track of what you’ve touched when making a lot of cross-cutting changes, like # @addsearch Then when you think you’re done with a change, you can run a quick search to see if you forgot to fix anything.

    Sometimes in VoodooPad notes, I’ll write in placeholders like that comment when I don’t have a bit of information yet, and now I can follow my trails the same way I do in code.

    Thanks, Gus!

     
  • PyObjC notes

    mike 11:07 am on February 1, 2008 | 0 Permalink

    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.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
esc
cancel