title: some criteria peg CPU using Scripting Bridge and NSPredicate
body: <p>I'm trying to get a list of tracks out of iTunes via Scripting Bridge. I'm using <code>NSPredicate</code> because that's <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ScriptingBridgeConcepts/UsingScriptingBridge/UsingScriptingBridge.html#//apple_ref/doc/uid/TP40006104-CH4-SW12" rel="nofollow">the recommended way</a>. This works very well in some cases, and is unusably slow in others. For instance, this will execute very quickly:</p>
  
  <pre><code>NSString *formatString = @"artist == ABC AND album == XYZ";
  NSPredicate *trackFilter = [NSPredicate predicateWithFormat:formatString];
  NSArray *tracksToPlay = [[libraryPlaylist fileTracks] filteredArrayUsingPredicate:trackFilter];
  </code></pre>
  
  <p>(<code>libraryPlaylist</code> is an iTunesLibraryPlaylist object that was created elsewhere.)</p>
  
  <p>But if I add either <code>kind</code> or <code>videoKind</code> to the mix, iTunes hits 100% CPU for a minute or more.</p>
  
  <pre><code>NSString *formatString = @"artist == ABC AND album == XYZ AND kind != 'PDF document' AND videoKind == %@", ;
  NSPredicate *trackFilter = [NSPredicate predicateWithFormat:formatString, [NSAppleEventDescriptor descriptorWithTypeCode:iTunesEVdKNone]];
  NSArray *tracksToPlay = [[libraryPlaylist fileTracks] filteredArrayUsingPredicate:trackFilter];
  </code></pre>
  
  <p>But that will eventually work. The real failure is <code>albumArtist</code>. If I try</p>
  
  <pre><code>NSString *formatString = @"albumArtist == ABC AND album == XYZ";
  NSPredicate *trackFilter = [NSPredicate predicateWithFormat:formatString];
  NSArray *tracksToPlay = [[libraryPlaylist fileTracks] filteredArrayUsingPredicate:trackFilter];
  </code></pre>
  
  <p>iTunes will go to 100% CPU and sit there for I don't know how long. (I gave up after 3 or 4 minutes.) Am I missing something or is this a bug in iTunes?</p>
  
  <h3>Additional info</h3>
  
  <p>My code takes the resulting tracks and calls another method to add them to a playlist (also using Scripting Bridge). I noticed when trying to filter by kind, the tracks would slowly pop onto the list one by one while iTunes hammered the CPU. This can only mean that <code>filteredArrayUsingPredicate</code> has already returned its results, so what is iTunes working so hard on?</p>