Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6666927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:52:47+00:00 2026-05-26T02:52:47+00:00

At the moment I have spotlight-api code which searches email’s body. I’m using NSMetadataQuery

  • 0

At the moment I have spotlight-api code which searches email’s body. I’m using NSMetadataQuery and creating predicate for "kMDItemTextContent like[c] %@". This works fine when requested “search-term” is in body of email.

In Spotlight App (magnifier icon in top right) if I enter “to: john” I’ll get list of emails in which “to” field contains word “john” (e.g. part of some email address john@something.com).

I tried to achieve this with [NSCompoundPredicate orPredicateWithSubpredicates:] by adding additional predicates of type "kMDItemRecipients", "kMDItemRecipientEmailAddresses", "kMDItemAuthors", "kMDItemAuthorEmailAddresses" and "kMDItemSubject".
Unfortunately this doesn’t return desired emails.

Does anyone know how to achieve this by using Spotlight-API?

Below is my code for this:

NSString *predicateFormat = @"kMDItemTextContent like[c] %@";
NSPredicate *predicateToRun = [NSPredicate predicateWithFormat:predicateFormat, self.searchKey];

NSString *predicateFormat1 = @"kMDItemTitle like[c] %@";
NSPredicate *predicateToRun1 = [NSPredicate predicateWithFormat:predicateFormat1, self.searchKey];

NSString *predicateFormat2 = @"kMDItemAuthorEmailAddresses like[c] %@";
NSPredicate *predicateToRun2 = [NSPredicate predicateWithFormat:predicateFormat2, self.searchKey];

NSString *predicateFormat3 = @"kMDItemAuthors like[c] %@";
NSPredicate *predicateToRun3 = [NSPredicate predicateWithFormat:predicateFormat3, self.searchKey];

NSString *predicateFormat4 = @"kMDItemRecipientEmailAddresses like[c] %@";
NSPredicate *predicateToRun4 = [NSPredicate predicateWithFormat:predicateFormat4, self.searchKey];

NSString *predicateFormat5 = @"kMDItemRecipients like[c] %@";
NSPredicate *predicateToRun5 = [NSPredicate predicateWithFormat:predicateFormat5, self.searchKey];

NSString *predicateFormat6 = @"kMDItemSubject like[c] %@";
NSPredicate *predicateToRun6 = [NSPredicate predicateWithFormat:predicateFormat6, self.searchKey];

NSUInteger options = (NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption);
NSPredicate *compPred = [NSComparisonPredicate
                         predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"*"]
                         rightExpression:[NSExpression expressionForConstantValue:self.searchKey]
                         modifier:NSDirectPredicateModifier
                         type:NSLikePredicateOperatorType
                         options:options];

predicateToRun = [NSCompoundPredicate orPredicateWithSubpredicates:
                     [NSArray arrayWithObjects:
                      compPred, 
                      predicateToRun, predicateToRun1, predicateToRun2, predicateToRun3, predicateToRun4,   
                      predicateToRun5, predicateToRun6, nil]];

predicateToRun = [NSCompoundPredicate andPredicateWithSubpredicates:
                  [NSArray arrayWithObjects:predicateToRun, [NSPredicate predicateWithFormat:@"(kMDItemContentType != 'public.vcard')"], nil]];

[self.query setPredicate:predicateToRun]; 

[self.query startQuery];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:52:48+00:00Added an answer on May 26, 2026 at 2:52 am

    I know how to do this with MDQuery – which in my opinion is simpler.

    You can use basically the same queries as you can use in mdfind from the command line.

    make a search string like: (NOT tested)

    ((((kMDItemAuthorEmailAddresses == "*john*"cd)) || ((kMDItemAuthors == "*john*"cd))) && (kMDItemContentType == 'com.apple.mail.emlx'))
    

    also in terminal
    mdls /path/to/library/mail/v2/24324.emlx
    will show what to search on for emails. Its your friend

    Note how you can hook up objective c notifications.

    NSString* query = some string ;
    
    MDQueryRef mdQuery = MDQueryCreate(nil, (CFStringRef)query, nil, nil);
    
    // if something is goofy, we won't get the query back, and all calls involving a mil MDQuery crash. So return:
    if (mdQuery == nil)
        return;
    
    NSNotificationCenter* nf = [NSNotificationCenter defaultCenter];
    [nf addObserver:self selector:@selector(progressUpradeQuery:) name:(NSString*)kMDQueryProgressNotification object:(id) mdQuery];
    [nf addObserver:self selector:@selector(finishedUpradeQuery:) name:(NSString*)kMDQueryDidFinishNotification object:(id) mdQuery];
    [nf addObserver:self selector:@selector(updatedUpradeQuery:) name:(NSString*)kMDQueryDidUpdateNotification object:(id) mdQuery];
    
    // Should I run this query on the network too? Difficult decision, as I think that this will slow stuff way down.
    // But i think it will only query leopard servers so perhaps ok
    //MDQuerySetSearchScope(mdQuery, (CFArrayRef)[NSArray arrayWithObjects:(NSString*)kMDQueryScopeComputer, (NSString*)kMDQueryScopeNetwork, nil], 0);
    
    // start it
    BOOL queryRunning = MDQueryExecute(mdQuery, kMDQueryWantsUpdates); 
    if (!queryRunning)
    {
        CFRelease(mdQuery);
        mdQuery = nil;
        // leave this log message in...
        NSLog(@"MDQuery failed to start.");
        return;
    }
    

    Tom

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I have the following code which works fine. label = new
At the moment I have about 2000 trades which are priced using excel. I
At the moment I have this (standard) code which gives me a full-width background
At the moment I have listings enabled(see code below) which means everyfolder is affected.
At the moment I have code to fade brightness adjustments which looks something like
At the moment I have a jQuery do a POST to a Controller which
At the moment I have this code (and I don't like it): private RenderedImage
At the moment I have two (maybe more) unordered lists which are sortable with
At the moment I have ResultsCollection = List<MyDataStructure>; which is then analysed with LINQ
At the moment I have a Ruby on Rails application which maintains my Users,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.