• 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: led248 <email@hidden>
  • Date: Thu, 27 Dec 2007 18:50:00 -0800

Thank you, HAS! You are absolutely correct. Apple's sample code does work IF I change the NSPredicate so that it finds at least one match.

Because the compiler throws a warning on Apple's sample code, I assumed that the runtime error was its logical erroneous result, and I didn't give much credence to the output. It never would have crossed my mind that zero matches would result in an error! And the sample code will almost certainly error out -- unless you happen to have a mounted disk whose name starts with 'M". You gotta be kidding.

Now that I know that the code (kinda) works, and I will have to tack on error check gobbledygook, I still don't know what that compiler warning is about:

SBElementArray *desiredDisks = [[finder disks] filteredArrayUsingPredicate:startsWithM];
warning: initialization from distinct Objective-C type


If I initialize desiredDisks as an NSArray (as signatured in the header file) the warning goes away. But then the next line will fail because arrayByApplyingSelector: will only work on an SBElementArray, not an NSArray:

NSArray *nameArray = [desiredDisks arrayByApplyingSelector:@selector(name)];
warning: 'NSArray' may not respond to '-arrayByApplyingSelector:'


After spending a little time with Leopard Scripting Bridge, I've gotta say that I'm pretty disappointed. It's just not ready for prime time -- language, tools, docs, even the overall premise (and promise) seems pretty flimsy. I'll have to spend some time with appscript in its various flavors, or just continue to make do with, sigh... AppleScript.

Anyone having a better time with SB?



On Dec 27, 2007, at 4:16 AM, has wrote:

led248 wrote:

Anybody using Scripting Bridge with filteredArrayUsingPredicate ???

I've tried Apple's example code -- it's the last code snippet in
"Scripting Bridge Release Notes for Mac OS X v10.5"    http://developer.apple.com/releasenotes/ScriptingAutomation/RN-ScriptingBridge/index.html

But it does not work.
[...]
   NSPredicate *startsWithM = [NSPredicate
predicateWithFormat:@"name BEGINSWITH 'M'"];

   SBElementArray *desiredDisks = [[finder disks]
filteredArrayUsingPredicate:startsWithM];
   NSArray *nameArray = [desiredDisks
arrayByApplyingSelector:@selector(name)];
[...]
   ErrorNumber = -1728;


Error -1728 = object(s) not found. It's up to individual applications to decide how to behave in response to a given command, and for whatever reason, the Finder elects to raise an error rather than return an empty list if the 'name of (every disk whose name starts with "M")' query fails to match anything. By comparison, a similar query in TextEdit, e.g. 'name of (every document whose name starts with "d")', would return an empty list on no matches. Blame Apple for failing to provide scriptable application developers with adequate design guidelines, particularly in the pre-SIG days. As a result, it's up to users to figure out exactly how individual applications' scripting interfaces behave in order to control them effectively.

In this case, you can either trap the error raised and recover accordingly, or use the 'exists' command to check if the query will match anything before trying to obtain the actual data. e.g. Using objc-appscript:

   // To create a Finder glue:  osaglue FN Finder

   FNApplication *finder;
   FNReference *ref;
   NSArray *diskNames;

finder = [[FNApplication alloc] initWithBundleID: @"com.apple.finder"];

ref = [[[finder disks] byTest: [[FNIts name] beginsWith: @"M"]] name];

   if ([[[ref exists] send] boolValue])
       diskNames = [[ref get] send];
   else
       diskNames = [NSArray array];

   NSLog(@"%@", diskNames);

   [finder release];

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


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

  • Prev by Date: "deviceTypeForPath:"
  • Next by Date: Re:NSPrintInfo will not accept paper format wider than high?
  • Previous by thread: Re: Scripting Bridge && filteredArrayUsingPredicate
  • Next by thread: Re: Scripting Bridge && filteredArrayUsingPredicate
  • Index(es):
    • Date
    • Thread