gpt4 book ai didi

cocoa - 使用脚本桥创建 iTunes 播放列表

转载 作者:行者123 更新时间:2023-12-03 16:05:03 25 4
gpt4 key购买 nike

我正在尝试使用 cocoa 脚本桥创建一个新的用户播放列表,但似乎无法让它工作。到目前为止我已经:

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];
}

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

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];
[playlist setName:playlistName];
}

另一个选择是使用 initWithProperties: 根据您对另一个答案的评论,这就是您最终要做的事情。

关于cocoa - 使用脚本桥创建 iTunes 播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1968794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com