Result of NSMetadataQuery using predicateFromMetadataQueryString: is wrong

I'm trying to get an NSMetadataQuery to work using an NSPredicate build with:

[NSPredicate predicateFromMetadataQueryString:@"InRange(kMDItemFSCreationDate,$time.today,$time.today(+1))"]

That string is the one I copied from Finder's Get Info window after saving a search of "Created is Today". It at least parses and runs without exceptions, but the results are totally wrong. It only returns files created on 12/31/2000.

I've tried dozens of different attempts at building a predicate that uses these $time functions, but they all fail parsing and throw an exception.

What I really need to be able to do is build the predicate with a format string, because the attribute is stored in a property:

[NSPredicate predicateWithFormat:@"InRange(%K,$time.today,$time.today(+1))", self.attr];

That throws an exception.

[NSPredicate predicateFromMetadataQueryString:[NSString stringWithFormat:@"InRange(%@,$time.today,$time.today(+1))", self.attr]];

That does not throw an exception, but it gives the same 12/31/2000 results.

Too bad nobody answered you. Six years later, I have a similar issue.

I can't get NSPredicate +predicateFromMetadataQueryString: to work with InRange operator using dates either. In my case my app gets no results.

But if I send the queryString to Finder via NSWorkspace +showSearchResultsForQueryString: Finder displays some results (though it appears inaccurate, as I know there are more files with a created date matching 'Today' than Finder is displaying).

Instead of using InRange, this string works for me:

kMDItemFSCreationDate >= $time.today && kMDItemFSCreationDate < $time.today(1)

What's weird is if I send that string to Finder via NSWorkspace it does not work in Finder at all.

So I guess the answer is to avoid using InRange with dates? There's not too much talk/examples for these old but still important APIs which enhance apps in meaningful ways!

Yeah, it's very, very too bad so many questions go unanswered around here. I'd have to search my project folder for that to even remember which app this was for. But that's an interesting workaround. I'll have to try it, if my problem is even still valid after all this time.

Result of NSMetadataQuery using predicateFromMetadataQueryString: is wrong
 
 
Q