Evil Hack of the Day

Evil Hack of the Day

MacOS plist XML files are evil; even more so than regular XML. For instance, my iTunes library file consists mostly of entries like:

<key>5436</key>
<dict>
	<key>Track ID</key><integer>5436</integer>
	<key>Name</key><string>Getting Better</string>
	<key>Artist</key><string>The Beatles</string>
	<key>Composer</key><string>Paul McCartney/John Lennon</string>
	<key>Album</key><string>Sgt. Pepper's Lonely Hearts Club Band</string>
	…
</dict>

You’ll notice that there’s no connection between a key and its value, other than proximity. There’s no real indication that these are fields in a data record, and unlike most XML files, you have to consider the position of each element compared to its neighbors. It’s almost as if someone took a file of the form

Track_ID = 5436
Name = "Getting Better"
Artist = "The Beatles"
Coposer = "Paul McCartney/John Lennon"

and, when told to convert it to XML in the name of buzzword-compliance, did a simple and quarter-assed search and replace.

But of course, what was fucked up by (lossless) text substitution can be unfucked by text substitution. And what’s the buzzword-compliant tool for doing text substitution on XML, even crappy XML? XSLT, of course. The template language that combines the power of sed with the terseness of COBOL.

So I hacked up an XSLT template to convert my iTunes library into a file that can be required in a Perl script. Feel free to use it in good or ill health. If you spring it on unsuspecting developers, please send me a photo of their reaction.

One thought on “Evil Hack of the Day

  1. Yeesh, that is the ugliest XML format I’ve ever seen. I’d argue that it’s not even a proper XML format because, as you point out, there’s no proper connection between each key and its value. I suppose it’s way too much to ask for something along the lines of [song][trackid]5436[/trackid][name]Getting Better[/name][artist]The Beatles[/artist][composer]McCartney/Lennon[/composer][album]Sgt. Pepper’s Lonely Hearts Club Band[/album][/song] ?

    (Replace square brackets with angle brackets appropriately, and insert your own formatting).

  2. Cyde Weys:
    Yeah, if I were designing this, I would’ve gone with something along the lines of

    <track>
      <id type="integer">5436</id>
      <name type="string">Getting Better</name>
      <last-played type="datetime">2010-10-03T17:14:36Z<last-played>
    </track>
    

    But no one asked me.

  3. jferg:
    Given that similar plists seem to be all over the place in MacOS, I’m guessing that they’re a fucked-up incarnation of something primeval, that so many things depend on that everyone would rather slap another compatibility library on top of, than to try to fix the right way.

Comments are closed.