A little bit of PyObjC

Well, Jason gave me a bit of a challenge on his Twitter, and so that got me to doing something I'd meant to do for a while.

I've wanted to mess around with the new Python bindings, and so I booted up Leopard and settled in with XCode. My goal was to duplicate a demonstration of Scripting Bridge that Wolf had done at PSIG, except in Python.

I've been a Pythonista for some time, and I like the interactivity mixed with the ability to create substantial applications.

What do I think? I really like it. I think my development focus is going to be on Leopard and beyond, for the excellent combination of Cocoa and Python it offers.

First off, Scripting Bridge takes all the hassle out of talking to another application. I've worked with AppleEvents for years, and it's always been a hassle, building up descriptors, and all the half-baked helpers that have previously existed.

One thing that's missing from Scripting Bridge for interpreted languages is that the introspection can't see the four character codes for enumerations. That's easily solved with a little bit of copy and edit from the .h file made by sdp, so I include that in the code as a demonstration.

It's fun to load up iPython to tinker around interactively with a scriptable application. iPython has tab completion for expressions, so it's easy to find out the names of various scripting commands. Unlike Objective-C, you can tinker with the expressions interactively until you get them the way you want them, then put them into your main Python code in your app. I'll put up a tutorial showing how to do that later.

So attached, you'll find the code for iTunesBridged, a Python-based Cocoa app for Xcode 3.0 and Leopard.
It shows the simple use of an application controller, Scripting Bridge, and Cocoa Bindings.

AttachmentDateSize
[file] iTunesBridged.zip14/01/08 1:40 am37.69 KB

Comments

Nice work

The app looks nice, good work Kevin. I wonder if it would be possible to invoke the sdp tool on the fly and parse the resulting header file to define the constants at runtime. Better yet, Apple should just fix ScriptingBridge to support this for us.

Looking forward to your next post!

If you're not already aware

If you're not already aware of it, you might be interested to know that track change notifications can be obtained automatically via NSDistributedNotificationCenter - very handy for keeping your own app's UI display synched with iTunes without the need for crude polling. Notification Watcher is a great little tool for sniffing out what gets sent when.

Re. using Scripting Bridge from Python: have you tried py-appscript at all? It's much more polished and robust than SB (enumerators are properly supported, amongst other things) and gives you a nice native Python API to work with. If you're an iPython fan, you'll also find its built-in help system terrific for exploring application dictionaries and objects interactively.

HTH

has
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org