[Pythonmac-SIG] iTunes query

Nicholas Riley njriley at uiuc.edu
Sun Jul 27 22:31:07 CEST 2008

On Sun, Jul 27, 2008 at 01:50:52PM -0400, David Blackman wrote:
> I'm trying to figure out how AOEM queries work on iTunes, the code I'm
> trying looks like:
>    for track in
> library.tracks[appscript.its.name().encode("utf-8").find("Sun")]:
> (I'm trying to find all the tracks in my library with the string "Sun"
> in the title)
> what am I doing wrong?

You're trying to use Python string and iterable methods on an
appscript reference.  Because the remote process is doing the query,
you're limited to specifying which it knows how to evaluate.  Once you
specify the query, you can then call .get() or just invoke the last

I generally find these things are easier to express in AppleScript
(scary, I know), so I use ASTranslate.  For example, if I type into

tell app "iTunes" to get tracks whose name contains "Sun"

I get back:


And the expression is ready to use:

In [7]: app(u'/Applications/iTunes.app').tracks[its.name.contains(u'Sun')].name()
[u'California Sun',
 u'Wasted Early Sunday Morning',
 u'My Sundown',
 u'The Sun Is Burning',

although you're better off using app(id='com.apple.iTunes') of course.

Nicholas Riley <njriley at uiuc.edu> | <http://www.uiuc.edu/ph/www/njriley>

More information about the Pythonmac-SIG mailing list