Mac Spotlight-API: how to search email's “to”, “from” or “subject” fields

寵の児 提交于 2019-12-08 13:39:27

问题


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:


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



来源:https://stackoverflow.com/questions/7674688/mac-spotlight-api-how-to-search-emails-to-from-or-subject-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!