上一篇post,在python里使用Scripting Bridge与iTunes交互,简单写了一下,这次是一个稍微实用一点的工具。
主要想法是过滤比特率在某一限度之下,且表演者的名字中含特定字符串的音乐。
其中NSNotFound的值就是NSIntegerMax,于是就直接写了。另外rate是以kbps做为单位计算的。
from Foundation import *
from ScriptingBridge import *
import sys
import getopt
iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
NSNotFound = 9223372036854775807
albums = set("")
rate = 320
artist = None
try:
opts, args = getopt.getopt(sys.argv[1:],'a:r:h',['artist=','rate=','help'])
for o, a in opts:
if o in ("-h", "--help"):
print'Usage: python-itunes.py [-a Artist] [-r Rate]'
sys.exit()
elif o in ("-r", "--rate"):
rate = int(a)
elif o in ("-a", "--artist"):
artist = unicode(a,'utf-8')
except getopt.GetoptError:
print'Usage: python-itunes.py [-a Artist] [-r Rate]'
sys.exit()
for track in iTunes.sources()[0].playlists()[0].tracks():
bitRate = track.bitRate()
if bitRate < rate:
if artist != None:
if track.artist().nsstring().rangeOfString_(artist).location != NSNotFound:
albums.add(track.album())
else:
albums.add(track.album())
for album_name in albums:
print album_name
其实仅仅看这个实现的话,是可以用iTunes的智能播放列表来实现的。不过用python的话还可以加入正则表达式,还可以联网做一些操作。(嘛,虽然一般人操作音乐库也不会弄得这么麻烦)