1

ScriptingBridge 経由で iTunes と対話するアプリケーションを作成しようとしています。これまでのところうまく機能していますが、この方法のオプションは非常に限られているようです。

特定の名前の曲を再生したいのですが、これを行う方法がないようです。iTunes.h に似たようなものは見つかりませんでした…</p>

AppleScript では、わずか 3 行のコードです。

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

すると、iTunes がクラシック ビートルズの曲の再生を開始します。ScriptingBridge でこれを行うことができますか、それともアプリからこの AppleScript を実行する必要がありますか?

4

1 に答える 1

4

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* を返します。ヘッダー ファイルを編集して、結果のコンパイラ警告を取り除くことができます。

于 2012-02-22T01:55:39.843 に答える