它不像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資料庫。然後使用腳本橋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 *。您可以編輯頭文件以擺脫由此產生的編譯器警告。
是的,它並不像AppleScript版本那麼簡單,但它非常棒!謝謝! – Chris 2012-02-23 18:51:38
請注意,對於那些下面的方法二(至少我的iTunes.h)庫應該是libraryPlayList,並且kSrS應該用單引號,或者甚至更好地使用itunes.h定義的枚舉:iTunesESrASongs – mackworth 2013-05-26 18:24:33