Question

I need solution for osx 10.6.7+. I trying to solve problem of searching "old" files on the disk. Old Files I mean files which early 1 year. I've created NSPredicate but NSMetadataQuery returns nothing

NSPredicate * fileTypePredicate = [NSPredicate predicateWithFormat: @"file_type == \"audio\""];
NSPredicate * accessDatePredicate = [NSPredicate predicateWithFormat: @"%K <= %@",  @"kMDItemAccessedDates", timeYearBefore];
return [NSCompoundPredicate andPredicateWithSubpredicates: @[fileTypePredicate, accessDatePredicate]];

Instead of kMDItemAccessedDates also I used acess_date but also without success.

Was it helpful?

Solution

For the first one, I don't know why you expect “file_type” to be a supported metadata property.

There are two properties for an item's content type. One is kMDItemContentType, which is the immediate type of the item's content (generally as determined by its filename extension). The other is kMDItemContentTypeTree, which is an array of that type and every one of its ancestors.

To find audio files, you want to find items whose kMDItemContentTypeTree is equal to kUTTypeAudio. (Yes, equality testing will work for this, even though you really want to test whether the array contains the type. Try it in Terminal: mdfind 'kMDItemContentTypeTree == public.audio')

In your code, you should use a two-parameter format string, just like you have for the second predicate, but with == as the operator: @"%K == %@" For the parameters, pass kMDItemContentTypeTree (the key) and kUTTypeAudio (the value—in this case, content type—you're looking for).


As for the second one, I can't find any mention of kMDItemAccessedDates anywhere other exactly one other Stack Overflow question. I think the author of that question might have made that key up for his custom Spotlight importer; you can't expect to find it on a stock OS X system.

You might try kMDItemLastUsedDate instead. (Don't write it as a string literal, in @"…"—just have kMDItemLastUsedDate without any quotes around it or an @ in front of it.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top