Getting results from Spotlight Saved Searches
Getting results from Spotlight Saved Searches
- Subject: Getting results from Spotlight Saved Searches
- From: Mark Allan <email@hidden>
- Date: Mon, 18 May 2009 22:17:05 +0100
Hello all,
I'm implementing something which requires me to perform various tasks
on a collection of files supplied by the user. I'd like to be able to
offer my users the ability to select a ".savedSearch" file and perform
those tasks on the files which result from that search.
What I'm doing so far is reading the .savedSearch file into an
NSDictionary, obtaining from that the "RawQueryDict", extracting the
values for "RawQuery" and "SearchScopes", and I'm using those to setup
an MDQueryRef. When I execute the query, it works, but I get back
many more files than are visible when the query is run in the Finder
(ie when the user double clicks their .savedSearch file).
The extra items all appear to be coming from within the Library
directory, such as <searchScope>/Library/Caches and Application
Support, which I'm fairly sure the Finder will never show in search
results. Without me having to modify the RawQuery string to add
filename/path checks, does anyone know how I can exclude the extra
files and just show exactly what the Finder shows?
There *are* two other keys in the RawQueryDict - FinderFilesOnly and
UserFilesOnly - both with a value of "true" so I'm assuming this is
where my answer lies, but I don't know where to add these into the
query. I've tried various ways of including them in the raw query
string but that usually leaves me with 0 results!
Here's the code I'm working with just now:
// Get the file, query string and search scope
NSDictionary *plistContents = [NSDictionary
dictionaryWithContentsOfFile:savedSearchFile];
NSDictionary *rawQueryDict = [plistContents
valueForKey:@"RawQueryDict"];
NSString *theQuery = [rawQueryDict valueForKey:@"RawQuery"];
NSArray *theScope = [rawQueryDict objectForKey:@"SearchScopes"];
// Create and execute the query
MDQueryRef query;
query = MDQueryCreate(kCFAllocatorDefault, (CFStringRef)theQuery,
NULL, NULL);
MDQuerySetSearchScope(query, (CFArrayRef)theScope, 0);
MDQueryExecute(query, kMDQuerySynchronous);
// Loop through and print the paths to see why I'm getting so many
results!
NSLog(@"results found %d",MDQueryGetResultCount(query));
for (int j=0; j<MDQueryGetResultCount(query); j++) {
MDItemRef item = MDQueryGetResultAtIndex(query, j);
NSLog(@"result %d: %@",j,(NSString *)MDItemCopyAttribute(item,
kMDItemPath));
}
Any help would be greatly appreciated.
Many thanks
Mark
_______________________________________________
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