Skip navigation.
 
mlRe: NSPredicate: To be, or not to be
FROM : stephen joseph butler
DATE : Tue Jun 03 04:59:37 2008

On Mon, Jun 2, 2008 at 7:37 PM, Gerriet M. Denkmann
<<email_removed>> 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.

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

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *value = [NSString stringWithCString:argv[ 1 ]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"%K LIKE[cd] %@",
                              kMDItemTextContent,
                              value];
    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];

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

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

    NSUInteger count = 0;
    for (NSMetadataItem *item in [query results])
    {
        ++count;

        NSLog( @"%d:", count );
        for (NSString *attribute in [item attributes])
        {
            NSLog(
                  @"\t%@: %@",
                  attribute,
                  [item valueForAttribute:attribute]
            );
        }
        NSLog(
              @"\t%@: %@",
              kMDItemPath,
              [item valueForAttribute:(NSString*)kMDItemPath]
        );
    }

    [pool drain];
    return 0;
}

Related mailsAuthorDate
mlNSPredicate: To be, or not to be Gerriet M. Denkman… Jun 1, 16:17
mlRe: NSPredicate: To be, or not to be stephen joseph but… Jun 1, 17:24
mlRe: NSPredicate: To be, or not to be Gerriet M. Denkman… Jun 2, 04:10
mlRe: NSPredicate: To be, or not to be stephen joseph but… Jun 2, 07:20
mlRe: NSPredicate: To be, or not to be Gerriet M. Denkman… Jun 2, 18:08
mlRe: NSPredicate: To be, or not to be stephen joseph but… Jun 2, 22:30
mlRe: NSPredicate: To be, or not to be Gerriet M. Denkman… Jun 3, 02:37
mlRe: NSPredicate: To be, or not to be stephen joseph but… Jun 3, 04:59
mlRe: NSPredicate: To be, or not to be Gerriet M. Denkman… Jun 3, 10:18
mlRe: NSPredicate: To be, or not to be Hamish Allan Jun 3, 10:33
mlRe: NSPredicate: To be, or not to be Gerriet M. Denkman… Jun 3, 11:23
mlRe: NSPredicate: To be, or not to be Jason Wiggins Jun 3, 16:27
mlRe: NSPredicate: To be, or not to be Hamish Allan Jun 3, 17:16