3/06/2003

Generators and Python/AppleEvents

I started writing some code in aetypes that adds iteration support to DelayedComponentItem today. Basically the idea is you would be able to do this:

import mail
m = mail.mail()

for msg in m.messages:
print msg.subject

That's just pseudocode, but you get the idea. All of the necessary moving parts are already in place to do this, as Jack Jansen noted in this message from 1999:

http://mail.python.org/pipermail/pythonmac-sig/1999-August/001245.html

The bits that I'm doing differently are thanks to Python 2.2's generators, which make it very easy to write support for things like "for foo in bar".

Here's what I'm doing:

I added a __getattr__ to TalkTo that uses the _moduleName attribute I added last year to look for the given name in the correct module. If one is found, I then return a DelayedComponentItem with an additional parameter passed -- self, or the TalkTo instance itself. This allows the DelayedComponentItem to call count, which is a generic AppleEvent which returns the number of items of that type in the specified container (which can be None, to mean the application itself)

Then, I added a __len__ method to DelayedComponentItem that calls count to see how many items of that type there are.

Finally, I added an __iter__ method that does len(self) and returns actual ComponentItem instances for each item in the container. It works perfectly!

The only thing that tripped me up was the fact that AppleEvents like to count things starting with 1 and Python counts starting with 0. So the first ComponentItem returned won't actually be useful for anything and will generate an error if you try to use it. I realized this while I was driving home, and I look forward to fixing it tomorrow morning!

3/05/2003

Scripting Mac OS X with Python using AppleEvents is accepted

Well, my OSCON proposal about scripting Mac OS X applications was accepted. Coincidentally, just yesterday I wrote a long piece about the discoveries I had made over the past year that would improve gensuitemodule to the MacPython mailing list. I think the ball is really going to start rolling this year. A lot more people are interested in Python, Mac OS X, and scripting, and Python has huge untapped potential in this area. I think the lack of documentation of the Python AppleEvent modules along with the generally arcane AppleEvent architecture prevented all but the most daring (me) from even attempting to do anything.

Good things are afoot.

Switched to MoveableType

Well, since MoveableType for my wife on our web host, I figured I might as well switch pyx to it -- at least until I finish Blog enough to want to use it for real. It's pretty close with all the features I added a couple weekends ago, but not close enough yet. It seems like working in concentrated spurts is a good way for me to get things done. I'll think about how I want things to be for a while, and when I get around to doing it I'll just whip it out really fast. Developing in Woven is turning out to be a real joy -- the code is clean enough that I can leave it lying around for months and come back to it with no problem, adding features easily.