husk.org / blog. chaff. occasional witterings.

2003-02-24

AppleEvents, subscription, and iTunes

computing 12:29:12

I've recently been playing with iTunes and AppleScript quite a bit, for the wire-and-string remote project (of which more soon, once I get a workable server out of Tom). One of the things I've noticed is that there's no way of asking for AppleEvents.

Perhaps it's best to describe why I'd want that in the first place. I'm writing something that monitors the state of iTunes, and in particular when it's playing a new track. Of course, there are plenty of applications that already do this. However, they all have to do the same thing; repeatedly ask iTunes what it's playing and act when that changes. Some of them, notably Kung-Tunes, can rely on network latency to reduce the poll interval, but PTHiTunesNotifier (and, as it happens, my app) have to ask iTunes about every two seconds.

As it turns out, this isn't too much of a processor drag, especially as iTunes is so heavy itself. But even so, shouldn't there be a neater way of doing this?

Apple itself gives us the answer. In the Cocoa programming tool kit, Matt Webb pointed out, there's an NSNotificationCenter (thanks, Peter) object. This works as a publication/subscription hub, which sounds complex, but is quite straightforward: objects let the hub know what events they're interested in. When an event happens, the object that handles the event merely forwards it to the hub, which then forwards it on to the next applications that want to know.

The analogous AppleEvent pubsubhub would, then, take messages from applications like iChatStatus saying "I'd like to know when iTunes moves on to the next track". iTunes would message the hub when this happened, and the hub would pass that on to any interested applications, hence cutting down the amount of AppleEvent traffic to a bare minimum.

This is ripe for a third party to develop, although of course iTunes and Mail.app - the prime candidates for sending events - would need Apple's support to get it working. Even so, a pubsubhub that could handle NetNewsWire sending events every time your favourite sites were updated and quietly opening browser windows minimized in the dock, for example, might get the ball rolling.

2003-03-13

clutter from eggwhite

trackback 12:11:39

I nominally try to keep this a blog where I ramble about various technical issues, but I want to point

2003-02-25

Peter Lindberg

comment 04:55:09

I agree. This rhymes with the mantra that Apple keep chanting at WWDC's: "on OS X, polling is bad". The iTunesRemote widget that comes with Konfabulator consumes a lot of CPU simply because it polls iTunes like crazy, as do lots of other such apps. I really like iChatStatus, but I feel uneasy about having it update every five seconds.

(By the way, NSNotification isn't the hub -- NSNotificationCenter is.)

2003-02-25

paul mison

comment 09:39:33

Thanks for the correction. That's what I get for writing the entry without checking the Cocoa docs. (Matt got this right when he talked to me, naturally.)

navigation.