使用脚本桥创建iTunes播放列表 [英] create iTunes playlist with scripting bridge

查看:147
本文介绍了使用脚本桥创建iTunes播放列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用可可脚本桥创建一个新的用户播放列表,但似乎不能让它工作。我到目前为止:

I am trying to create a new user playlist using the cocoa scripting bridge, but cannot seem to get it to work. I have so far:

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];
    [playlist setName:playlistName];
    [[library userPlaylists] insertObject:playlist atIndex:0];
}

当我尝试添加播放列表的名称时, :

When I try and add a name for the playlist, I get the error message:


iTunesBridge [630:80f] *** - [SBProxyByClass setName:]:object尚未添加到容器中;

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

推荐答案

>解决方案

错误消息告诉你Scripting Bridge对象,如你的播放列表,在添加到相关的SBElementArray之前不能接收消息,所以你试图在播放列表上设置一个属性将其添加到数组失败。

The error message is telling you that Scripting Bridge objects like your playlist can't receive messages until they've been added to the relevant SBElementArray, so your attempt to set a property on the playlist before adding it to the array fails.

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

The simplest solution is just to rearrange the last two lines of code, like this:

// if the playlist was not found, create it
if (!playlist) {
    playlist = [[[iTunes classForScriptingClass:@"playlist"] alloc] init];
    [[library userPlaylists] insertObject:playlist atIndex:0];
    [playlist setName:playlistName];
}

另一个选项是使用 initWithProperties: code>根据你对另一个答案的评论是你最后做的。

The other option is to use initWithProperties: which according to your comment on another answer is what you ended up doing.

这篇关于使用脚本桥创建iTunes播放列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆