2

Using the iTunes Scripting Bridge interface, I need to fetch a particular iTunesTrack by its persistentID. I closely examined the header file produced sdp/sdef but it looks like there is no method in the existing interface for performing any kind of query for a track based on any parameter. The next idea I had was to enumerate all of the tracks examining each for a match.

The implication is that this would be slow; the larger the target library the worse the performance. I'm wondering if anyone has a proven solution to this problem that doesn't involve examining every track returned from the scripting bridge, one at a time?

At get a specific track in itunes via ScriptingBridge, Arr MiHardies states that he came up with a solution and would post it but apparently, he never got around to it.

Community
  • 1
  • 1
SMJ
  • 21
  • 1
  • 2

1 Answers1

0

The trick is to get the entire iTunes library as an array, then use Cocoa's NSPredicate filtering to find what you want.

iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
SBElementArray *iTunesSources = [iTunes 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];    
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"persistentID == %@", persistentID];
[musicTracks filterUsingPredicate:predicate];

In general it's often a good rule of thumb to do as little as possible with Scripting Bridge methods—use them to get the data out, then use normal Cocoa methods for everything else.

Gabriel Roth
  • 1,030
  • 1
  • 12
  • 31