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. 开发者_如何学Python A little help appreciated.
Disclaimer: OS/X newbie here.
Note: I am not looking for ways to access the XML/plist database directly.
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.
What do you think of:
script = '''tell application "iTunes"
repeat with bTrack in (every track of playlist 7 whose artist contains "Golden")
print bTrack
end repeat
end tell'''
tracks,_ = subprocess.Popen("osascript -e %s" % script, stdout=subprocess.PIPE).communicate()
trackList = tracks.split('\n')
I never tested this, though....
It might be useless answer, but i'd advise you to use AppleScript instead of Python. Take a look at this piece of code:
tell application "iTunes"
repeat with bTrack in (every track of playlist 7 whose artist contains "Golden")
...
Perfect tutorials might be found here: http://macscripter.net/viewtopic.php?id=25631
精彩评论