Skip to content

Instantly share code, notes, and snippets.

@robert-chiniquy
Created March 27, 2012 17:56
Show Gist options
  • Save robert-chiniquy/2218378 to your computer and use it in GitHub Desktop.
Save robert-chiniquy/2218378 to your computer and use it in GitHub Desktop.
Grab track rating information from itunes library xml file and write to itunes using applescript
#! /usr/bin/env -i python
import xml.etree.ElementTree
import subprocess
tree = xml.etree.ElementTree.ElementTree()
tree.parse('iTunes Music Library.xml')
doc = tree.getroot()
top = doc.find('dict')
tracks = top.find('dict')
# <key>Date Modified</key><date>2004-08-31T23:17:43Z</date>
# <key>Date Added</key><date>2004-08-19T18:17:13Z</date>
# <key>Play Count</key><integer>3</integer>
# <key>Play Date</key><integer>-1117685658</integer>
# <key>Rating</key><integer>60</integer>
sets = [
# "Date Modified",
# "Date Added",
# "Play Count",
# "Play Date",
"Rating"
]
for track in tracks.findall('dict'):
cur = None # prop being currently parsed, alternately, cursor
props = track.findall('*')
name = track.find('string').text # first string is name?
for prop in list(props):
if cur == None and prop.text in sets:
cur = prop.text
elif cur in sets:
cmd = [#"echo",
"osascript", "-e", """tell application "iTunes" to set the %s of the track named "%s" to %s""" % (cur, name, prop.text)]
try:
print ' '.join(cmd)
except:
pass # non-ascii error
subprocess.call(cmd)
cur = None
#exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment