开发者

How do I find iTunes library folder on Mac and Windows?

I made an application that parse the iTunes library to retrieve its content. It works fine in most cases but if a user moved his library somewhere else than the default iTunes folder (see: http://lifehacker.com/238296/ultranewb--how-to-move-your-itunes-library-to-an-external-drive), then I need a way to find this path.

On Mac, I was looking into ~/Library/Preferences/com.apple.iTunes.plist. There is a setting called "alis:1:iTunes Library Location" but it contains several parameters all concatenated and converted to hexadecimal.

On Windows, I found this file "C:\Documents and Settings\\Application Data\Apple Comput开发者_开发问答er\iTunes\iTunesPrefs.xml" that contains a setting "iTunes Library XML Location:1" but this one is encoded.

Any help would be greatly appreciated. Thanks!


On Windows, the iTunes Library XML Location:1 entry in iTunesPrefs.xml is a Base 64 encoded Unicode string, so you'll need to decode it before you can use it. On my PC, it decodes to C:\Documents and Settings\Emerick\My Documents\My Music\iTunes\iTunes Music Library.xml.

It should be relatively easy to decode this value using your language of choice; your platform may even provide utility libraries that make this trivial. In C#, for example, the decoding function would look something like this:

static public string DecodeBase64(string encodedData)
{
  byte[] encodedBytes = System.Convert.FromBase64String(encodedData);
  return System.Text.UnicodeEncoding.Unicode.GetString(encodedBytes);
}


I can't help you with the Windows stuff, but on the Mac what you're seeing in that prefs file is old-school alias handle data. Take a look at or just use Chris Hanson's BDAlias class to convert it to a path.

http://github.com/rentzsch/bdalias


As the others point out "alis:1:iTunes Library Location" is alias data. Here's how I find the path from the data in OS X using Python.

#!/usr/bin/env python

import commands, plistlib
from Carbon import File
from os.path import expanduser

PLIST_PATH = '~/Library/Preferences/com.apple.iTunes.plist'
PLIST_KEY = 'alis:1:iTunes Library Location'

def resolve_path_from_alias_data( alis ):

    fs_ref = File.Alias( rawdata=alis ).FSResolveAlias( None )[0]
    file_path = fs_ref.as_pathname()
    return file_path

plist_str = commands.getoutput( '/usr/bin/plutil -convert xml1 -o - "' + expanduser( PLIST_PATH ) + '"' )

plist_data = plistlib.readPlistFromString( plist_str )

alis_data = plist_data[ PLIST_KEY ].data

file_path = resolve_path_from_alias_data( alis_data )

print repr( file_path )

Unfortunately, iTunes no longer uses "alis:1:iTunes Library Location" so this no longer works. Now iTunes 11 uses an entry called "RDoc:132:Documents" which seems to be completely different. I have posted a similar question with the appropriate iTunes 11 details.

Actually, my answer works just fine as of OS X 10.9.1. I'm not sure whether it stopped due to some error I made, or if Apple quietly reverted something. Either way, it's working again on my Mac.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜