我正在尝试使用可可脚本桥创建一个新的用户播放列表,但似乎无法使其工作.我到目前为止
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:根据你对另一个答案的评论是你最终做的.