0

In Objective-C, I am setting up an NSMetaDataQuery and setting the setSearchScope: for the query to search a specific users Documents folder. The search works but doesn't recurse down the directory tree, only searching in the Documents folder.
I've tried wildcards but no joy.

Here is basically what I've attempted, and it works except not searching below the Documents directory:

query = [[NSMetadataQuery alloc] init];
[query setSearchScopes:[NSArray arrayWithObjects:@"/Users/username/Documents/",nil]];
[query setPredicate:[NSPredicate predicateWithFormat:@"(kMDItemFSContentChangeDate >= '$time.this_week')"]];
[query startQuery];
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Wilersh
  • 85
  • 1
  • 5

2 Answers2

1

I think that the problem is with your NSPredicate format string. (I have been meaning to write this up!) Essentially, your $time.this_week would work great in a Spotlight query, but not as a NSPredicate string. You have to actually create an NSDate object and pass it into the query string like

[query setPredicate:[NSPredicate predicateWithFormat:@"(kMDItemFSContentChangeDate <= %@)", [NSDate date]]];

Here are the relevant links for your reference:

Comparison of NSPredicate and Spotlight Query Strings
Spotlight Query Expression Syntax
Predicate Format String Syntax

jxpx777
  • 3,632
  • 4
  • 27
  • 43
0

I think your problem might be the trailing slash on /Users/username/Documents/. When dealing with paths in Objective-C, the framework never puts on a trailing slash. Does taking it off help?

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Good thought. I did try taking it off and got the same results. I've been thinking there may be something I can do with NSString defining the path, but haven't solve it yet. There are many methods for managing paths in NSString which I discovered while working through this. – Wilersh Nov 10 '10 at 20:56
  • http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/ManipulatingPaths.html%23//apple_ref/doc/uid/20000152-BBCBIGHH – Wilersh Nov 10 '10 at 20:56