Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a track on iTunes

I'd like to search for tracks on iTunes using a Python script on Mac OS/X. I found a way to access the iTunes application through:

iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")

but I haven't figured out (yet) the way to perform searches. A little help appreciated.

Disclaimer: OS/X newbie here.

Note: I am not looking for ways to access the XML/plist database directly.

like image 424
jldupont Avatar asked Oct 15 '22 06:10

jldupont


1 Answers

You might want to check out appscript (note, you'll need ASDictionary for online help):

>>> import appscript
>>> iTunes = appscript.app("iTunes")
>>> lib = iTunes.playlists['Library']
>>> for trk in lib.tracks():
...     if re.search("test", trk.name()):
...         print trk.name()

This might give you the most control by iterating over each item, but there's a much faster way too, by using applescript hooks to do the searching:

>>> trks = lib.tracks[appscript.its.name.contains('test')]
>>> print trks.name()

Check out these appscript usage examples as well.

like image 157
James Snyder Avatar answered Oct 19 '22 02:10

James Snyder