Created
April 1, 2010 01:59
-
-
Save gonzoua/351217 to your computer and use it in GitHub Desktop.
Getting location of iTunes media folder in Cocoa. requirements: BDAlias
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// newer iTunes seems to prefer "iTunes Media" over "iTunes Music" | |
#define ITUNES_MUSIC @"/Music/iTunes/iTunes Music" | |
#define ITUNES_MEDIA @"/Music/iTunes/iTunes Media" | |
-(NSString*) _getiTunesMediaFolder | |
{ | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
[defaults addSuiteNamed:@"com.apple.iTunes"]; | |
NSDictionary *keys = [defaults dictionaryRepresentation]; | |
NSString *key; | |
NSData *aliasData = nil; | |
NSString *mediaFolder = nil; | |
for (key in keys) | |
{ | |
if ([key hasPrefix:@"alis:"] && [key hasSuffix:@"Music Folder Location"]) | |
{ | |
aliasData = [defaults dataForKey:key]; | |
break; | |
} | |
} | |
// try to get dir from alias | |
if (aliasData) | |
{ | |
mediaFolder = [[BDAlias aliasWithData:aliasData] fullPath]; | |
if (mediaFolder) | |
NSLog(@"iTunes Media Folder location: %@", [mediaFolder stringByAbbreviatingWithTildeInPath]); | |
return mediaFolder; | |
} | |
// check standard locations | |
BOOL isDir; | |
NSString *homeDir = NSHomeDirectory(); | |
if ([[NSFileManager defaultManager] fileExistsAtPath:[homeDir stringByAppendingString:ITUNES_MEDIA] | |
isDirectory:&isDir]) | |
{ | |
if (isDir) | |
return [[homeDir stringByAppendingString:ITUNES_MEDIA] autorelease]; | |
} | |
if ([[NSFileManager defaultManager] fileExistsAtPath:[homeDir stringByAppendingString:ITUNES_MUSIC] | |
isDirectory:&isDir]) | |
{ | |
if (isDir) | |
return [[homeDir stringByAppendingString:ITUNES_MEDIA] autorelease]; | |
} | |
// whatever. homedir is good enough | |
return homeDir; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment