iTunes XML File

Hi again scripters!

A friend sent me her iTunes Library’s XML file, so I can find some new songs I might like in it. I’m looking to build a script (using text item delimiters likely) to go in and grab JUST a list of songs from all the mayhem. Just to make sure I’m not missing the obvious, is there a simple way to do it? Thanks for the help, building the script the way I’m planning will take ages.

There are lots of ways to skin a cat. Here is one way. :wink:

Save this on your desktop as ‘read_xml.rb’
You can change what you are retrieving out of the XML file
by changing the word ‘Name’ below to another XML tag.


#!/usr/bin/env ruby -w

require 'cgi'

puts `grep 'Name' '#{ARGV[0]}'`.map { |name| puts CGI.unescapeHTML(name.scan(/.+<string>(.+)<\/string>/)[0][0]) }

Or you could use this if you are getting lots of ‘nil’ items at the end.


#!/usr/bin/env ruby -w

require 'cgi'

`grep 'Name' '#{ARGV[0]}'`.each do |name| 
  exit if name == nil
  puts CGI.unescapeHTML(name.scan(/.+<string>(.+)<\/string>/)[0][0]) 
end

You can call it from AppleScript like this.

set thePathXML to "/posix/path/to/iTunes Music Library.xml"
set rubyPath to "/posix/path/to/read_xml.rb"

set trackNames to paragraphs of (do shell script "/usr/bin/ruby " & quoted form of rubyPath & space & quoted form of thePathXML)

Cheers,

Craig

That worked beautifully, thanks! I’ve never messed around with Ruby before, do you know a good place to find a tutorial?

Hi Jamie,

I posted some links here.

Also, here is a list of programming books I own.

Let me know if you need help.

Craig

Thanks for the help Craig. You have a well designed site, very impressive.