Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve iTunes preferences programmatically to find audio libraries?

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 directory path(s) from iTunes. How would I retrieve the directory paths(s) from iTunes? Would this be in some sort of preference setting?

Thanks.

like image 875
David Avatar asked Nov 27 '25 20:11

David


1 Answers

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
like image 101
David Avatar answered Dec 01 '25 02:12

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!