• 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: NSPredicate: To be, or not to be
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSPredicate: To be, or not to be


  • Subject: Re: NSPredicate: To be, or not to be
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Tue, 3 Jun 2008 15:18:32 +0700


On 3 Jun 2008, at 11:52, email@hidden wrote:

On Mon, Jun 2, 2008 at 7:37 PM, Gerriet M. Denkmann <email@hidden> wrote:
On 3 Jun 2008, at 03:30, stephen joseph butler wrote:

I'm sorry. I forget that the Spotlight predicate strings are slightly
different from the regular ones. This works for me:


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE
%@", kMDItemTextContent, @"To be, or not to be"];

This one also works for me. Only it kind of works too well, finding thousands of files.

Another example: <kMDItemTextContent LIKE "Briggel Braggel"> finds
".../Test.txt" which only contains the line: "Briggel and Braggel" .
But I really want only files which contain "Briggel Braggel" or "the Briggel
Braggel of today".


Again: How to create a predicate for an 10.4.11 NSMetadataQuery to find a
string which includes blanks.
Possible answers:
Escape the blanks with ..., or
Enclose whole string with ..., or something else ?

For me, the following only finds one match when run with "To be, or not to be" (a file from Fink)... not the thousands you're getting. And if I create only one file on my system with "Briggel and Braggel", I get no hits for "Briggel Braggel" (and only one for the correct phrase).

I'm not sure what's different about your code, but I suspect the
problem isn't the NSPredicate.

I modified your example slightly (to make it palatable to Tigers) and it behaves quite differently to what you are seeing.


Output:
Finding: "Briggel Braggel"
Predicate: <kMDItemTextContent LIKE[cd] "Briggel Braggel">
1: "/Volumes/เม่น/Users/gerriet/Desktop/Briggel and Braggel.txt"
2: "/Volumes/เม่น/Users/gerriet/Library/Mail/.../Sent Messages.mbox/Messages/52191.emlx"
3: "/Volumes/เม่น/Users/gerriet/Library/Mail/Mailboxes/ Lists/Dev.mbox/Messages/52203.emlx"


The first file (Briggel and Braggel.txt) contains only the line: "Briggel and Braggel".

Maybe Tiger behaves diffently to Leopard (at least Spotlight-wise)?

So: Any suggestions for a Tiger-solution?

Here is my code:

#import <Foundation/Foundation.h>
#include <CoreServices/CoreServices.h>

int main (int argc, const char * argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	if (argc != 2)
	{
	    NSLog( @"usage: %s <query>", argv[ 0 ] );
	    return 1;
	}

	NSString *value = [NSString stringWithCString:argv[ 1 ]];
	NSLog(@"Finding: \"%@\"", value);

	NSPredicate *predicate = [NSPredicate predicateWithFormat:
	                          @"%K LIKE[cd] %@",
	                          kMDItemTextContent,
	                          value];
	NSLog(@"Predicate: <%@>", predicate);
	NSMetadataQuery *query = [[NSMetadataQuery alloc] init];

	[query setPredicate:predicate];
	[query startQuery];

	while ([query isGathering])
	    [[NSRunLoop currentRunLoop]
	     runMode:NSDefaultRunLoopMode
	     beforeDate:[NSDate distantFuture]];

	unsigned resultCount = [ query resultCount ];
	for (unsigned i = 0; i < resultCount; i++ )
	{
		NSMetadataItem *item = [query resultAtIndex: i ];
		NSString *s = [item valueForAttribute:(NSString*)kMDItemPath];
		NSLog( @"%5u: \"%@\"", i + 1 ,s );
	}

	[pool release];
	return 0;
}

Kind regards,

Gerriet.

_______________________________________________

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


  • Prev by Date: Re: Hillegass book memory question
  • Next by Date: Re: NSPredicate: To be, or not to be
  • Previous by thread: Re: NSPredicate: To be, or not to be
  • Next by thread: Re: Graphing With Google Charts API
  • Index(es):
    • Date
    • Thread