Jay Tuley

Posts tagged with itunes

iTunes Music Library.xml by Jay

So someone asked me a question, and I’ve had this question before, so I decided to answer it in my blog.

Tom asks:

One thing that’s been confusing me for awhile is locating the XML file for the iTunes library. The iTunes library can be in an arbitrary location, so relying on it being in ~/Music/iTunes/ is bound to fail for anyone who relocates their library.

My question is, in iEatBrainz, how do you extract the location of the iTunes library from the iTunes preferences, which are guaranteed to be in a defined location. From looking at it, iTunes seems to encode a Classic-style ‘alis’ resource into the data fork, but I can’t figure out how to turn that back into something I can manipulate from Cocoa.

It’s a pretty good question, when I originally looked into locating the iTunes Music Library.xml file (for the Music Recommendation System), I also was under the impression that the file could be in arbitrary places just like Tom. However, I soon discoved that there are only specific places that file can exist. It had only seemed like this wasn’t the case because the default location for iTunes Music Folder is the same as where your library files need to be. So if you look for the library file at ~/Music/iTunes/iTunes Music Library.xml, you’ll be okay even if the Music Folder is set to be somewhere else.

So that’s easy enough, but wait, you may now be wondering did If I really mean “places” when I wrote “specific places” and indeed there is another place your library could be located, ~/Documents/iTunes/iTunes Music Library.xml. iTunes checks in precedence for the iTunes folder, first ~/Music, if it doesn’t find it there it checks in /Documents. If all you have is the iTunes folder /Documents it will use the library there and save and update it, if you have both, it will use ~/Music. If you have neither it create a folder in ~/Music. Another thing you should do to make sure that you find the iTunes Music Library.xml file, is because some people tend to movie things around with aliases, you need to resolve aliases in that path, you can do that with this code from Apple.

So that’s basically all you have to do to find the iTunes Music Library.xml file. And if for some reason you program still can’t find the xml file you can pop up an open dialog box.

There are some other notes about the xml file worth noting, it’s periodically updated, and seems to be in place for Cocoa apps like iMovie, iPhoto, and iDVD, however none of them check for the file in ~/Documents (so if you look in the the secondary location, you and iTunes are already one step in sync better than Apple’s own iApps). The tracks are also numbered inside the XML with a Track ID, and in AppleScript they are referred to as “Database ID”, these values aren’t constant, and every so often they get renumbered, it’s worth noting as this has been the bane of iEatBrainz in the past and present.

Finally, while it doesn’t concern most, in my opinion there is a major bug regarding the dates in the XML file. The XML file uses the GMT offset for the daylight savings time that you are currently observing, not the offset for when that date occurred in time.

So on this day of posting I’m currently not observing daylight savings time. So that’s 6 hours behind GMT where my thinks it’s located.

Song A was added in November (not DTS, thus 6 hours behind)
2001-11-27T01:35:27Z (-600 = 7:35:00 PM) in XML
Monday, November 26, 2001 7:35:27 PM in Applescript and GUI

Which is right, everything matches, however:

Song B with a Data added in July, thus in DTS
(5 hours behind GMT in my location)
2001-07-18T20:24:29Z (-500 = 3:24:29 PM) in XML
Wednesday, July 18, 2001 2:24:29 PM in Applescript and GUI

That’s wrong! the XML doesn’t match up with the GUI and AppleScript!

If I change my current time to a day in daylight savings time:
2001-07-18T19:24:29Z (-500 = 2:24:29 PM) in XML

iTunes rewrote the date, and now the time matches the GUI, however all the non DTS dates now don’t match!

It’s just frustrating, I filed a bug report with Apple last June, but I’m not sure if they care as iMovie, iDVD, and iPhoto, don’t care about dates.

So yeah, I guess that’s the iTunes Music Library.xml file in a nutshell.

iEatBrainz applescript to add iTunes selection by Jay

I received an email a little earlier asking what iEatBrainz applescript commands would allow you to add songs from an iTunes selection. iEatBrainz only has a applescript dictionary because it’s an Apple Script Studio app with a lot of Obj-C and just a small bit of applescript. However, since the applescript dictionary encompasses almost all of cocoa, it wasn’t too hard to write a script that takes the currently selected song in iEatBrainz and add it to the tagging list (As long as you know what methods to call).

Open this script in Script Editor

on addSongWithDatabaseID(aDatabaseID, aLibrary, aTaggingController)
    
tell application “iEatBrainz”
        
set aTrack to call method “trackForTrackID:” of aLibrary with parameter (aDatabaseID as string)
        
call method “addiTunesTrack:” of aTaggingController with parameter aTrack
    
end tell
end
addSongWithDatabaseID

tell application “iEatBrainz”
    
set myLibrary to call method “musicLibrary” of call method “mediator” of call method “delegate” of application
    
    set
taggingController to call method “musicMatchings” of call method “mediator” of call method “delegate” of application
end tell

tell
application
“iTunes”
    
set these_tracks to the selection of browser window 1
    —
if no selection
    
if these_tracks is {} then error “No tracks are selected in the front window.”
end tell

repeat with
i from 1 to number of items in these_tracks
    
set this_item to item i of these_tracks
    
tell application “iTunes”
        
set dbID to database ID of this_item
    
end tell
    
addSongWithDatabaseID(dbID, myLibrary, taggingController)
end repeat

This still will have problems on occasion when iEatBrainz gets out of sync with iTunes, due to iTunes renumbering its database.