iTunes script: Album Shuffle
It strikes me that what I need is an Album Shuffle to choose for me. Here's a first cut: iTunesAlbumShuffle.py. (Requires: iTunes 4.5 or above; Python 2.4.x or above plus the pywin32 extensions -- or just get the ActivePython distribution.) Run once to populate an album's-worth of tracks into an Album Shuffle playlist, which it'll create if necessary.
Ideally it'd stick around and watch what's being played, removing played albums and adding new albums as needed; but those are version 2 features.
At the scripting level, you get a glimpse into iTunes internals. Sources, playlists and tracks are what iTunes really deals in at the database level. Albums are an abstraction which iTunes, and iTunes scripters, have to build by aggregating tracks with common Artist and Album attributes. That's why this script has a long startup time: it has to walk the entire iTunes library to determine what albums are present.
A quicker method might be to pick a random track from the library and then do an IITPlaylist::Search to find other tracks in the same album. Pitfalls await the unwary here, though. IITPlaylist::Search can match either on all track attributes, or on one and one only of the track attributes. In either case, you'd need to filter the search results to flush out any false positives. And selecting with this method skews the random choice by weighting albums with many tracks heavier than albums with few tracks. (The current code gives an equal weighting to all albums; possibly the fairest method would weight albums by the inverse of their length, allowing short-and-sweet singles a better chance than bulky doubles.)
And as I'm using Python 2.4, I get a chance to play with two new minor features: the sorted() builtin, and the key argument to list.sort() and sorted(). Nice. A lot of the sorting code I've written in the past could have be streamlined with these.
Oh and its first pick? Coldplay's A Rush Of Blood To The Head.