Search code examples
objective-ccocoaitunes

Scan iTunes Library?


I'm working on a Mac app and I need it to scan the users iTunes library. Any idea how I would go about this? The app would scan the library looking for different song attributes.


Solution

  • To find the actual most recent location of the iTunes library (and not hope for it to be at the default location) read the iTunesRecentDatabases or iTunesRecentDatabasePaths property from ~/Library/Preferences/com.apple.iApps.plist.

    NSArray *libraryDatabases = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"com.apple.iApps"] objectForKey:@"iTunesRecentDatabases"];
    NSURL *libraryURL = (([libraryDatabases count])) ? [NSURL URLWithString:[dbs objectAtIndex:0]] : nil;
    

    For a more advanced code snippet look for parserInstancesForMediaType: in iMedia's IMBiTunesParser.m


    For the actual database parsing it is recommended to use a SAX parser, such as NSXMLParser (vs. a tree parser, such as NSXMLDocument or even worse: NSPropertylistSerialization) as some users have librares with up to and sometimes even more than 100,000+ tracks. Using tree parsing or even serialization will seriously slow down and potentially block your app temporarily.