我正在尝试使用可可脚本桥创建一个新的用户播放列表,但似乎无法使其工作.我到目前为止
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"]; SBElementArray *iSources = [iTunes sources]; iTunesSource *library = nil; for (iTunesSource *source in iSources) { if ([[source name] isEqualToString:@"Library"]) { library = source; break; } } // could not find the itunes library if (!library) { NSLog(@"Could not connect to the iTunes library"); return; } // now look for our playlist NSString *playlistName = @"new playlist"; SBElementArray *playlists = [library userPlaylists]; iTunesUserPlaylist *playlist = nil; for (iTunesUserPlaylist *thisList in playlists) { if ([[thisList name] isEqualToString:playlistName]) { playlist = thisList; break; } } // if the playlist was not found,create it if (!playlist) { playlist = [[[iTunes classForScriptingClass:@"playlist"] alloc] init]; ; [[library userPlaylists] insertObject:playlist atIndex:0]; }
iTunesBridge[630:80f] *** -[SBProxyByClass setName:]: object has not been added to a container yet; selector not recognized
任何人都可以指向正确的方向吗?
解决方法
错误消息告诉您,像播放列表之类的Scripting Bridge对象在添加到相关的SBElementArray之前无法接收消息,因此在将播放列表添加到数组之前尝试设置属性失败.
// if the playlist was not found,create it if (!playlist) { playlist = [[[iTunes classForScriptingClass:@"playlist"] alloc] init]; [[library userPlaylists] insertObject:playlist atIndex:0]; ; }
另一个选择是使用initWithProperties:根据你对另一个答案的评论是你最终做的.