I\'m creating an application on the Mac in Cocoa that plays audio. I think the best place for my application to search for audio files on a user\'s computer is to get the direct
Just for those who will encounter this problem in the future, I've documented the approach that I used below.
I decided not to use the Karelia framework because I don't need all the bells and whistles. I just need to locate the iTunes library and display a list of songs.
I went through the Karelia framework and it appears that they locate the iTunes library via the user defaults as shown below.
+ (NSArray*) parserInstancesForMediaType:(NSString*)inMediaType
{
NSMutableArray* parserInstances = [NSMutableArray array];
if ([self isInstalled])
{
CFArrayRef recentLibraries = CFPreferencesCopyAppValue((CFStringRef)@"iTunesRecentDatabases",(CFStringRef)@"com.apple.iApps");
NSArray* libraries = (NSArray*)recentLibraries;
for (NSString* library in libraries)
{
NSURL* url = [NSURL URLWithString:library];
NSString* path = [url path];
BOOL changed;
(void) [[NSFileManager imb_threadSafeManager] imb_fileExistsAtPath:&path wasChanged:&changed];
NSString *libraryPath = [path stringByDeletingLastPathComponent]; // folder containing .xml file
[IMBConfig registerLibraryPath:libraryPath];
IMBiTunesParser* parser = [[[self class] alloc] initWithMediaType:inMediaType];
parser.mediaSource = path;
parser.shouldDisplayLibraryName = libraries.count > 1;
[parserInstances addObject:parser];
[parser release];
}
if (recentLibraries) CFRelease(recentLibraries);
}
return parserInstances;
}
Using the above approach, you can locate all iTunes libraries with the following code.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *userPref = [userDefaults persistentDomainForName:@"com.apple.iApps"];
NSArray *recentDatabases = [userPref objectForKey:@"iTunesRecentDatabases"];
for (NSString *key in recentDatabases)
{
NSLog(@"%@", key);
}
Also if you inspect the user defaults iApps dictionary you can use these other keys to get info for other applications
iMovie
iPhotoAutoImportPath
iPhotoRecentDatabases
iTunesRecentDatabases
iTunesRecentDatabasePaths
Edit: As others have pointed out, the approach below doesn't address the case that the iTunes library may not be in a user's home folder. See David's answer for a better solution. Finding the user's music folder may still be somewhat useful for finding a standard (though not the only) location of non-iTunes audio files (e.g. as the default folder for an open panel).
Original answer:
You could locate the user's Music folder via
NSString *musicPath = [NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES) objectAtIndex:0];
This will normally have an iTunes subdirectory that contains the file "iTunes Music Library.xml" which contains most metadata of the user's iTunes library. Don't try to write to it though - iTunes only creates this file so that other apps can access it, but doesn't use it internally otherwise.
Use the Scripting Bridge to talk to iTunes. You can ask iTunes about the user's library and every track in it; file tracks will freely tell you where they are in the file-system.
Alternatively, embed Karelia Software's iMedia Browser framework, which will talk to iTunes for you.
You probably should also be able to open audio files that the user drops on your app or assigns to it in Finder's Get Info and opens. Consider making your application document-based; alternatively, implement the right application delegate method and declare the right Info.plist properties.