通过 ScriptingBridge 在 iTunes 中播放特定标题

发布于 2025-01-07 11:39:47 字数 377 浏览 6 评论 0原文

我正在尝试编写一个通过 ScriptingBridge 与 iTunes 交互的应用程序。到目前为止我工作得很好,但这种方法的选择似乎非常有限。

我想播放给定名称的歌曲,但似乎没有办法做到这一点。我在 iTunes.h 中没有找到任何类似的东西...

在 AppleScript 中,它只有三行代码:

tell application "iTunes"
    play (some file track whose name is "Yesterday")
end tell

然后 iTunes 开始播放经典的披头士乐队歌曲。 我可以使用 ScriptingBridge 执行此操作,还是必须从我的应用程序运行此 AppleScript?

I'm trying to write an application that interacts with iTunes via ScriptingBridge. I works well so far, but the options of this method seem to be very limited.

I want to play song with a given name, but it looks like there's no way to do this. I haven't found anything similar in iTunes.h…

In AppleScript it's just three lines of code:

tell application "iTunes"
    play (some file track whose name is "Yesterday")
end tell

And then iTunes starts to play a classic Beatles song.
Is there any was I can do this with ScriptingBridge or do I have to run this AppleScript from my app?

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

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

发布评论

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

评论(1

夢归不見 2025-01-14 11:39:47

它不像 AppleScript 版本那么简单,但肯定是可能的。

方法一

获取指向 iTunes 库的指针:

iTunesApplication *iTunesApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
SBElementArray *iTunesSources = [iTunesApp sources];
iTunesSource *library;
for (iTunesSource *thisSource in iTunesSources) {
    if ([thisSource kind] == iTunesESrcLibrary) {
        library = thisSource;
        break;
    }
}

获取包含库中所有音频文件曲目的数组:

SBElementArray *libraryPlaylists = [library libraryPlaylists];
iTunesLibraryPlaylist *libraryPlaylist = [libraryPlaylists objectAtIndex:0];
SBElementArray *musicTracks = [self.libraryPlaylist fileTracks];    

然后过滤该数组以查找具有您要查找的标题的曲目。

NSArray *tracksWithOurTitle = [musicTracks filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K == %@", @"name", @"Yesterday"]];   
// Remember, there might be several tracks with that title; you need to figure out how to find the one you want. 
iTunesTrack *rightTrack = [tracksWithOurTitle objectAtIndex:0];
[rightTrack playOnce:YES];

方法二

获取指向 iTunes 库的指针,如上所示。然后使用 Scripting Bridge searchFor: only: 方法:

SBElementArray *tracksWithOurTitle = [library searchFor:@"Yesterday" only:kSrS];
// This returns every song whose title *contains* "Yesterday" ...
// You'll need a better way to than this to pick the one you want.
iTunesTrack *rightTrack = [tracksWithOurTitle objectAtIndex:0];
[rightTrack playOnce:YES];

对方法二的警告:iTunes.h 文件错误地声明 searchFor: only: 方法返回 iTunesTrack*,当事实上(出于明显的原因)它返回一个 SBElementArray*。您可以编辑头文件以消除生成的编译器警告。

It's not as simple as the AppleScript version, but it's certainly possible.

Method one

Get a pointer to the iTunes library:

iTunesApplication *iTunesApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
SBElementArray *iTunesSources = [iTunesApp sources];
iTunesSource *library;
for (iTunesSource *thisSource in iTunesSources) {
    if ([thisSource kind] == iTunesESrcLibrary) {
        library = thisSource;
        break;
    }
}

Get an array containing all the audio file tracks in the library:

SBElementArray *libraryPlaylists = [library libraryPlaylists];
iTunesLibraryPlaylist *libraryPlaylist = [libraryPlaylists objectAtIndex:0];
SBElementArray *musicTracks = [self.libraryPlaylist fileTracks];    

Then filter the array to find tracks with the title you're looking for.

NSArray *tracksWithOurTitle = [musicTracks filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K == %@", @"name", @"Yesterday"]];   
// Remember, there might be several tracks with that title; you need to figure out how to find the one you want. 
iTunesTrack *rightTrack = [tracksWithOurTitle objectAtIndex:0];
[rightTrack playOnce:YES];

Method two

Get a pointer to the iTunes library as above. Then use the Scripting Bridge searchFor: only: method:

SBElementArray *tracksWithOurTitle = [library searchFor:@"Yesterday" only:kSrS];
// This returns every song whose title *contains* "Yesterday" ...
// You'll need a better way to than this to pick the one you want.
iTunesTrack *rightTrack = [tracksWithOurTitle objectAtIndex:0];
[rightTrack playOnce:YES];

Caveat to method two: The iTunes.h file incorrectly claims that the searchFor: only: method returns an iTunesTrack*, when in fact (for obvious reasons) it returns an SBElementArray*. You can edit the header file to get rid of the resulting compiler warning.

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