AppleScript バージョンほど単純ではありませんが、確かに可能です。
方法 1
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];
方法 2
上記のように、iTunes ライブラリへのポインタを取得します。次に、スクリプト ブリッジ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];
メソッド 2 の警告: iTunes.h ファイルは、searchFor: only:
メソッドが iTunesTrack* を返すと誤って主張していますが、実際には (明らかな理由で) SBElementArray* を返します。ヘッダー ファイルを編集して、結果のコンパイラ警告を取り除くことができます。