2012-02-21 12 views
1

私はScriptingBridge経由でiTunesとやりとりするアプリケーションを作成しようとしています。これまでのところうまくいきますが、この方法の選択肢は非常に限られているようです。ScriptingBridge経由でiTunesで特定のタイトルを再生する

私は指定された名前の曲を再生したいですが、これを行う方法がないように見えます。

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

そしてiTunesは古典的なビートルズの曲を再生するために開始されます。私はそれは、コードのわずか3行だAppleScriptでは...

をiTunes.hで似た何かを発見していません。 ScriptingBridgeでこれを行うことができましたか、またはこのAppleScriptをアプリケーションから実行する必要がありますか?

答えて

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

、フィルタ配列を使用して、探しているタイトルのトラックを探します。 2

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ライブラリへのポインタを取得します。方法2へ

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

が警告:次にスクリプティングブリッジsearchFor: only:メソッドを使用iTunes.hが誤ってファイルは、実際には(明白な理由のために)それは* SBElementArrayを返すときsearchFor: only:方法は、iTunesTrack *を返すと主張しています。ヘッダーファイルを編集して、結果として生じるコンパイラの警告を取り除くことができます。

+0

AppleScriptのバージョンほどシンプルではありませんが、うまくいきます!ありがとう! – Chris

+0

2つの方法(少なくとも私のiTunes.hで)ライブラリはlibraryPlayListでなければならず、kSrSは一重引用符で囲むか、itunes.h定義の列挙型を使用する方が良いことに注意してください。iTunesESrASongs – mackworth

関連する問題