如何通过 ScriptingBridge 检索 iTunes 曲目?

发布于 2024-11-15 18:47:58 字数 839 浏览 12 评论 0原文

我有两种情况。给定歌曲的曲目 ID,将评级设置为某个整数。第二个是相同的,除了给我一个轨道 ID 数组。我知道我可以使用 ScriptingBridge 根据歌曲名称搜索 iTunesTrack 对象,但是有什么方法可以根据曲目 ID 获取它吗?大致如下:

iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
NSInteger *rating;
NSInteger *id;

if ( [iTunes isRunning] ) {
    iTunesTrack *track = [ iTunes trackForDatabaseID:id ];
    [ track setValue:rating forkey:@"rating" ];
}

对于第二种情况,有没有办法在给定曲目 id 数组的情况下检索 iTunesTrack 的 SBElementArray 对象?比如:

iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
NSArray *ids; //array of NSIntegers

if ( [iTunes isRunning] ) {
    SBElementArray *tracks = [ iTunes tracksForDatabaseIDs:ids ];
    [ tracks setValue:rating forkey:@"rating" ];
}

我相信这比根据歌曲名称迭代搜索库更有效。

I have two situations. Given a track id of a song, set the rating to some integer. The second is the same except I am given an array of track ids. I know I can use the ScriptingBridge to search for the iTunesTrack object based on a song's name, but is there some way to get it based off the track id? Something along the lines of:

iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
NSInteger *rating;
NSInteger *id;

if ( [iTunes isRunning] ) {
    iTunesTrack *track = [ iTunes trackForDatabaseID:id ];
    [ track setValue:rating forkey:@"rating" ];
}

For the second situation, is there a way to retrieve a SBElementArray object of iTunesTrack given an array of track ids? Something like:

iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
NSArray *ids; //array of NSIntegers

if ( [iTunes isRunning] ) {
    SBElementArray *tracks = [ iTunes tracksForDatabaseIDs:ids ];
    [ tracks setValue:rating forkey:@"rating" ];
}

I believe this would be more efficient than iteratively searching the library based on a song's name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2024-11-22 18:47:58

我最近刚做了这个。像这样的东西应该对你有用。您需要询问库播放列表(在此示例中名为libraryPlaylist)而不是应用程序。

NSArray *trackIDs = blah; // the ids you're searching for
NSString *searchFilter = @"databaseID == %@";
NSMutableArray *filters = [NSMutableArray arrayWithCapacity:[trackIDs count]];
for (int i = 0; i < [trackIDs count]; i++) {
    [filters addObject:searchFilter];
}
searchFilter = [filters componentsJoinedByString:@" OR "];
NSArray *trackResult = [[libraryPlaylist tracks] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:searchFilter argumentArray:trackIDs]];

如果 trackIDs 数组仅包含单个项目,则效果很好,因此无需为这种情况编写特殊代码。

I just did this recently. Something like this should work for you. You need to ask the library playlist (named libraryPlaylist in this example) and not the application.

NSArray *trackIDs = blah; // the ids you're searching for
NSString *searchFilter = @"databaseID == %@";
NSMutableArray *filters = [NSMutableArray arrayWithCapacity:[trackIDs count]];
for (int i = 0; i < [trackIDs count]; i++) {
    [filters addObject:searchFilter];
}
searchFilter = [filters componentsJoinedByString:@" OR "];
NSArray *trackResult = [[libraryPlaylist tracks] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:searchFilter argumentArray:trackIDs]];

And that works just fine if the trackIDs array only contains a single item, so there's no need to write special code for that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文