I am writing an app in Xcode and need to create a new playlist in iTUnes, and add specific tracks to it.
Here is the code I have so far:
In the code above, i'm not too picky about what tracks I add (I'll worry about that after I can add a track to the playlist) so for now it is just adding the first N tracks from the iTUnes main track library.
When I run the code above the error message contained in NSError is:
Any help here would be greatly appreciated.
Here is the code I have so far:
Code:
-(ITunesError *) savePlaylist:(Playlist *) playlist {
ITunesError * rv = [[ITunesError new]retain];
//Get name of playlist we want to create
NSString * name = [playlist name];
ITReference *playlistRef = [[itunes playlists] byName:name];
/* Find a unique name for playlist we are creating */
if([[[playlistRef exists]send] boolValue]){
int i = 1;
NSString * n = @"";
do{
n = [NSString stringWithFormat:@"%@_%d", name, i];
playlistRef = [[itunes playlists] byName:n];
i++;
}while([[[playlistRef exists]send] boolValue]);
name = n;
}
//Create new playlist in itunes
[[[[itunes make] new_: [ITConstant playlist]] withProperties: [NSDictionary dictionaryWithObject: name forKey: [ITConstant name]]] send];
//Returns the main music library for iTunes
ITReference *main = [self getRefToMainPlaylist];
ITReference * tracks = [main tracks];
//NSArray * trackNames = [[tracks name] getItem];
//NSArray * artistNames = [[tracks artist] getItem];
NSArray * locations = [[tracks location ] getItem];
NSMutableArray *files = [NSMutableArray array];
for(int j = 0; j < 10; j++){
ASAlias * alias = (ASAlias *) [locations objectAtIndex:j];
NSLog([alias path]);
NSURL *file = [NSURL fileURLWithPath: [alias path]];
[files addObject: file];
}
/* At this point here is what we have:
playlistRef - Reference to playlist we just created in itunes
files - Array of NSURL objects pointing to files on disk.
*/
NSError * error = 0;
[[[itunes add: files] to: playlistRef] sendWithError:&error];
if(error){
[rv setErrorObject:error];
NSLog([rv getErrorMessage]);
}
return nil;
}
In the code above, i'm not too picky about what tracks I add (I'll worry about that after I can add a track to the playlist) so for now it is just adding the first N tracks from the iTUnes main track library.
When I run the code above the error message contained in NSError is:
which comes from when I try to add the files to the playlist.Application error: the AppleEvent was not handled by any handler
Any help here would be greatly appreciated.