可可 – 使用脚本桥创建iTunes播放列表

前端之家收集整理的这篇文章主要介绍了可可 – 使用脚本桥创建iTunes播放列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用可可脚本桥创建一个新的用户播放列表,但似乎无法使其工作.我到目前为止
  1. iTunesApplication *iTunes = [SBApplication
  2. applicationWithBundleIdentifier:@"com.apple.iTunes"];
  3. SBElementArray *iSources = [iTunes sources];
  4. iTunesSource *library = nil;
  5. for (iTunesSource *source in iSources) {
  6. if ([[source name] isEqualToString:@"Library"]) {
  7. library = source;
  8. break;
  9. }
  10. }
  11.  
  12. // could not find the itunes library
  13. if (!library) {
  14. NSLog(@"Could not connect to the iTunes library");
  15. return;
  16. }
  17.  
  18. // now look for our playlist
  19. NSString *playlistName = @"new playlist";
  20. SBElementArray *playlists = [library userPlaylists];
  21. iTunesUserPlaylist *playlist = nil;
  22. for (iTunesUserPlaylist *thisList in playlists) {
  23. if ([[thisList name] isEqualToString:playlistName]) {
  24. playlist = thisList;
  25. break;
  26. }
  27. }
  28.  
  29. // if the playlist was not found,create it
  30. if (!playlist) {
  31. playlist = [[[iTunes classForScriptingClass:@"playlist"] alloc] init];
  32. ;
  33. [[library userPlaylists] insertObject:playlist atIndex:0];
  34. }

当我尝试添加播放列表的名称时,我收到错误消息:

iTunesBridge[630:80f] *** -[SBProxyByClass setName:]: object has not been added to a container yet; selector not recognized

任何人都可以指向正确的方向吗?

解决方法

错误消息告诉您,像播放列表之类的Scripting Bridge对象在添加到相关的SBElementArray之前无法接收消息,因此在将播放列表添加到数组之前尝试设置属性失败.

最简单的解决方案是重新排列最后两行代码,如下所示:

  1. // if the playlist was not found,create it
  2. if (!playlist) {
  3. playlist = [[[iTunes classForScriptingClass:@"playlist"] alloc] init];
  4. [[library userPlaylists] insertObject:playlist atIndex:0];
  5. ;
  6. }

另一个选择是使用initWithProperties:根据你对另一个答案的评论是你最终做的.

猜你在找的iOS相关文章