• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Scripting Bridge && filteredArrayUsingPredicate
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scripting Bridge && filteredArrayUsingPredicate


  • Subject: Re: Scripting Bridge && filteredArrayUsingPredicate
  • From: email@hidden
  • Date: Wed, 2 Jan 2008 23:31:33 +0100

So it's a Cocoa thing then?? Jesus...

theArtist = @"Beatles";
theAlbum = @"";

track_array = (SBElementArray *)[libraryPlaylist searchFor:theArtist
only:'kSrR']; //artists

// Filter by artist, we get a track in the array
predicate = [NSPredicate predicateWithFormat:@"artist like[cd] %@", theArtist];
track_array = (SBElementArray *)[track_array
filteredArrayUsingPredicate:predicate];

nameArray = [track_array arrayByApplyingSelector:@selector(name)];
NSLog(@"%@", nameArray); // => ("I Feel Fine")

// Filter again by album, no go...
predicate = [NSPredicate predicateWithFormat:@"album like[cd] %@", theAlbum];
track_array = (SBElementArray *)[track_array
filteredArrayUsingPredicate:predicate];

nameArray = [track_array arrayByApplyingSelector:@selector(name)];
NSLog(@"%@", nameArray); // =>  () Nothing here!!


On Jan 2, 2008 8:34 PM, has <email@hidden> wrote:
> On 2 Jan 2008, at 16:58, email@hidden wrote:
>
> >>> On a similar note, I recently stumbled upon what appears to be
> >>> another
> >>> major flaw in filteredArrayUsingPredicate:. If the predicate is
> >>> set to
> >>> match an empty string "" (a common enough operation when dealing
> >>> with
> >>> meta tags, for example in iTunes), filteredArrayUsingPredicate:
> >>> always
> >>> returns an empty array, regardless if the objects in the unfiltered
> >>> array matches the predicate or not.
> >>
> >> Seems to work here. One reason you might not get matches is if you're
> >> using an empty string where some other value is expected (integer,
> >> 'missing value' constant, etc).
> >
> > Really? How did you accomplish that? I'd love to see a working
> > example.
>
> The following works here, picking up all tracks that lack an album name:
>
> #####################
>
> #!/usr/bin/ruby
>
> require 'osx/cocoa'
> include OSX
> OSX.require_framework 'ScriptingBridge'
>
> iTunes =
> SBApplication.applicationWithBundleIdentifier("com.apple.itunes")
>
> the_album = ""
>
> track_array =
> iTunes.sources.objectAtIndex(0).playlists.objectAtIndex(0).tracks
>
> predicate = NSPredicate.predicateWithFormat("album like[cd] %@",
> the_album)
>
> track_array = track_array.filteredArrayUsingPredicate(predicate)
>
> puts track_array.arrayByApplyingSelector(:name)
>
> #####################
>
> (
>      "Track 01",
>      "Track 01",
>      "Track 02",
>      "Track 02",
>      "Track 03",
>      "Track 03",
>      "Track 04",
>      "Track 04",
>      "Track 05",
>      "Track 05",
>      ...
> )
>
> #####################
>
>
> If in doubt, check against AppleScript to see if the problem is with
> the bridge or with the target application, e.g.:
>
>         tell app "iTunes" to name of (every track whose album is "") of
> playlist 1 of source 1
>
>
> I'll also add a caveat that I don't yet know much about how SB applies
> predicates - I think it maps at least some tests to Apple event
> equivalents, but none of this stuff is publicly documented so it's
> hard to know what's going on behind the scenes.
>
>
> >> Anyway, my advice FWIW would be: 1. learn how Apple event IPC really
> >> works, and 2. use appscript to do it.
> >
> > Cool. I hadn't heard about appscript before. I'll play around with it
> > later and see how it compares.
>
>
> As I've said elsewhere, objc-appscript is still officially alphaware
> so the usual caveats apply. There's still a few implementation details
> to take care of; however, the basic architecture is already quite
> solid since it's based on the proven Python appscript design, and the
> high-level API is settling down now so there shouldn't be much more
> disturbance from your POV.
>
> For example:
>
> // To create iTunes glue: osaglue  -o ITGlue  -p IT  iTunes
>
> #import <Foundation/Foundation.h>
> #import "ITGlue/ITGlue.h"
>
> int main (int argc, const char * argv[]) {
>         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
>
>         ITApplication *itunes;
>         ITReference *ref;
>         NSError *err;
>         NSString *albumName;
>         NSArray *trackNames;
>
>         albumName = @"";
>
>         itunes = [[ITApplication alloc] initWithBundleID: @"com.apple.itunes"];
>
>         ref = [[[[[itunes playlists] at: 1] tracks] byTest: [[ITIts album]
> equals: albumName]] name];
>
>         trackNames = [[ref get] sendWithError: &err];
>
>         if (!trackNames)
>                 NSLog(@"Error: %@", err);
>         else
>                 NSLog(@"Result: %@", trackNames);
>
>         [itunes release];
>         [pool drain];
>         return 0;
>
> }
>
>
> HTH
>
> has
> --
> http://appscript.sourceforge.net
> http://rb-appscript.rubyforge.org
>
>
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Scripting Bridge && filteredArrayUsingPredicate
      • From: has <email@hidden>
References: 
 >Re: Scripting Bridge && filteredArrayUsingPredicate (From: has <email@hidden>)
 >Re: Scripting Bridge && filteredArrayUsingPredicate (From: email@hidden)
 >Re: Scripting Bridge && filteredArrayUsingPredicate (From: has <email@hidden>)

  • Prev by Date: Re: Localization Issue
  • Next by Date: Re: Status item using custom view not responding to clicks (Follow up)
  • Previous by thread: Re: Scripting Bridge && filteredArrayUsingPredicate
  • Next by thread: Re: Scripting Bridge && filteredArrayUsingPredicate
  • Index(es):
    • Date
    • Thread