Itunes Library Xml Path Mac



  1. Import Itunes Library Xml File
  2. Plex Itunes Library Xml Path Mac

At home my family uses Apple iTunes and Safari for the majority of our media playing on our TV. This simple setup runs on a 2010 Mac Mini that I purchased prior to us moving to Prague. It has worked very well as a bare-bones media server that the children know how to use without too much support from me. Recently, I decided to try and consolidate onto one mirrored 4TB drive the media that was on two separate drives (one internal, one external).

Choose File Library Import Playlist, locate the iTunes Library.xml file you copied earlier. ITunes will now examine the XML file, extracting all your tracks and playlists, and import them to your library. It will take some time to rebuild the database of music, especially if you have a huge iTunes library collection. Convert the Library.xml file from PC to Mac format. Now, the Library.xml file that you exported on the source contains all your music playlists and ratings, but it contains file paths specific to.

To perform the consolidation, I used rsync to copy from the two source drives to the new big drive. Though normally used to manage file collections among two or more separate machines, it offers a great collection of features for copying locally on the same machine. For instance, the macOS version contains the smarts to also sync any metadata and extended file system attributes associated with the media files. Two rsync commands left me with a new disc with all of the media intact and no errors.

Next, I started up iTunes while holding down the option key in order to bring up a prompt that allowed me to create a new library.

I then changed some advanced iTunes settings in order to use the new media disc drive and to let iTunes manage its contents. I also enabled the Share iTunes Library XML with other application setting so that I could perform some library manipulation described next.

Migrating Metadata

Since I created a new library above, I no longer had any metadata associated with media content such as play counts or ratings as these are held inside a specific iTunes library file. The next step was to copy over the metadata from the old iTunes library file. The Share iTunes Library XML with other application options mentioned above asks iTunes to generate and update an XML representation of the library. However changes made to this file will not appear in iTunes. Instead, one must rely on AppleScript functionality to make any updates to entity metadata (see below).

Python has a built-in library for reading in Apple XML files – plistlib. Although it seems to apply to just plist files (those with a suffix of .plist), it properly handles the .xml file that iTunes creates from its library contents. Using plistlib I was able to create a short script that migrated certain metadata values from the old library to the new one.

In an iTunes library each media entity (audio, movie, etc) is called a track and is given a unique integer. Unfortunately, these integer values are not the same across libraries. Therefore, I had to generate my own keys using track attributes that would not change across library instances yet would not collide with other tracks in the library. I chose a 5-tuple made up of the following attributes:

  • Name — track name
  • Album — collection name where the track resides
  • Total Time — measure of how long the media is in seconds
  • Size — measure of how large the media is in bytes
  • Location — location of the media file

The Album name protects against duplicate song names from different albums. The Total Time and Size use physical characteristics to further protect against name collisions. Finally the last component of the Location path protects against situations where there are duplicate audio files – something that should probably be cleaned up in the future. Note that only the last component can be assumed to be shared across the libraries; everything else in the path can be different.

Now that I have (hopefully) unique keys that will apply across iTunes libraries, I next build a mapping of these keys and track entities from the source library in order to find them with keys generated from the destination library.

(the check for “Voice Memo” Genre attribute removes collisions I had with voice memos from an iPhone — this was the easiest way to deal with them)

Import Itunes Library Xml File

AppleScript and Python

A long time ago, I made my own Python server to drive a SLiMP3 device. The server (code available here) parsed the iTunes library XML file to figure out what audio files were available, and it used AppleScript to control iTunes. To bridge between Python and AppleScript, the server relied on a wonderful package called appscript. Although development on appscriptstopped in 2012, amazingly it still works on my macOS Sierra (10.12.5) MacBook Pro.

Interacting with iTunes via appscript is surprisingly simple though there are times when lack of documentation makes for rough going. First, to get access to the library of media tracks in iTunes:

To get a subset of tracks, one needs to provide one or more criteria that tells iTunes which tracks to chose from all in the library. For instance, to get tracks with names containing the word 'Alien’:

Note that trailing get() call causes the query to execute in the iTunes app. Before that, what is held locally is a pending AppleEvent expression (which can be quite complex). We can create a similar query to get the track we want to update by looking for the Track ID found in the XML file. The corresponding AppleEvent property to compare against is database_ID.

Path

Plex Itunes Library Xml Path Mac

Here we fetch from iTunes the track(s) with the given Track ID value in their database_ID property. If we get anything but an array of one element, we assign 0 to the track variable as a signal that there is no track to work with.

Next, we handle each attribute with a custom iTunes AppleEvent set command. There are three kinds of settings we look for and apply:

  • Play/Skip counts and the date of the last play or skip if the count is non-zero
  • User ratings — integer values between 0 and 100 inclusive for a track or an album
  • Loved/Disliked flags — Boolean values assigned to a track or an album (NOTE: these are not always available)

Play and skip counts are handled in the same way, though the date of the last play has an unusual name since there was a legacy read-only ‘Play Date’ attribute in the XML schema. For ratings, we only set a value if there is not an associated computed property with a true value, which would indicate that the rating value was not set by the user but rather calculated by iTunes.

Full Script

Here in full is the script that I used. To run from the command line:

where SRC is the path to the source library XML file and DST is the path to the destination library XML file.