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

前端之家收集整理的这篇文章主要介绍了可可 – 使用脚本桥创建iTunes播放列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用可可脚本桥创建一个新的用户播放列表,但似乎无法使其工作.我到目前为止
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:根据你对另一个答案的评论是你最终做的.

原文链接:https://www.f2er.com/iOS/329980.html

猜你在找的iOS相关文章