• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: exporting a playlist from itunes as xml
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: exporting a playlist from itunes as xml


  • Subject: Re: exporting a playlist from itunes as xml
  • From: has <email@hidden>
  • Date: Sat, 29 Jul 2006 18:56:59 +0100

Adam Ochonicki wrote:

What I'd like to do is take an iTunes playlist and export certain ID3 elements of each track to an xml file. Does anyone know how to do this?

What's the structure of the XML file, and which ID3 tags? Anyway, the following Python script outputs the name, album, artist and duration for each track in the named playlist. The template markup/logic can be modified to suit. You'll need to install the appscript package from my site in order to use it.


======= START SCRIPT =======

#!/usr/bin/env pythonw

import os.path, sys
from appscript import *
from HTMLTemplate import Template

#######
# Template

templatemarkup = u"""<?xml version="1.0" encoding="UTF-8"?>

<playlist node="con:playlist" name="Some Playlist">
<track node="rep:track">
    <name node="con:name">Track Name</name>
    <album node="con:album">Album</album>
    <artist node="con:artist">Artist Name</artist>
    <duration node="con:duration">180</duration>
</track>
</playlist>
"""

def render_template(node, playlist):
node.playlist.atts['name'] = playlist.name.get()
ref = playlist.tracks
trackdescriptions = zip(ref.name.get(), ref.artist.get(), ref.album.get(), ref.duration.get())
node.playlist.track.repeat(render_track, trackdescriptions)


def render_track(node, (name, artist, album, duration)):
    node.name.content = name
    node.album.content = album
    node.artist.content = artist
    node.duration.content = str(duration)

#######
# Main

def write(path, txt):
    f = open(os.path.expanduser(path), 'w')
    f.write(txt.encode('utf8'))
    f.close()

if len(sys.argv) != 3:
    print 'Usage: pythonw playlist_to_xml.py playlist-name output-file'
    sys.exit()
playlistname, path = sys.argv[1:3]
template = Template(render_template, templatemarkup)
playlist = app('iTunes').playlists[playlistname]
xml = template.render(playlist)
write(path, xml)

======= END SCRIPT =======

(Note: it's a bit of a cheat since HTMLTemplate isn't a proper XML templating engine, but as long as tag names are all lowercase there shouldn't be a problem.)


To use it, save the above script as playlist_to_xml.py, then use Terminal or 'do shell script' to run a command like:


    pythonw playlist_to_xml.py 'playlist name' '/path/to/file.xml'


The resulting XML file will look similar to this:

<?xml version="1.0" encoding="UTF-8"?>

<playlist name="Recently Played">
<track>
    <name>Verklärte Nacht 7. Adagio</name>
    <album>Berg: Violin Concerto, Schoenberg Verklärte Nacht</album>
    <artist>BBC Symphony Orchestra Donald Runnicles</artist>
    <duration>245</duration>
</track>
...
</playlist>


If you really want to write it all in AppleScript though then I'd suggest using Satimage's XMLLib osax to do your XML generation.


HTH

has
--
http://freespace.virgin.net/hamish.sanderson/


_______________________________________________ Do not post admin requests to the list. They will be ignored. Applescript-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Prev by Date: Re: FileMaker Web Viewer
  • Next by Date: Re: FileMaker Web Viewer
  • Previous by thread: Re: exporting a playlist from itunes as xml
  • Next by thread: Claudio Braxmaier ist bis 21.08.2006
  • Index(es):
    • Date
    • Thread