Note that there are some explanatory texts on larger screens.

plurals
  1. PONSPredicate(s) for NSMetadataQuery to match kMDItemAccessedDates (array) containing a target date?
    text
    copied!<p><strong>UPDATE:</strong> I found an answer though there may still be a better way - see the answer added below.</p> <hr> <p>a spotlight importer imports metadata that is an array of dates in which an item was accessed. Looks like this:</p> <pre><code>kMDItemAccessedDates = ( "2012-06-18 07:00:00 +0000", "2013-01-10 08:00:00 +0000", "2013-01-15 08:00:00 +0000" ) </code></pre> <p>I'd like to query for any of these files that were accessed on a particular date the user enters but I'm not finding the right syntax to do this. Wondering if it's possible to do...</p> <p>The first issue is that the user enters a date which represents a day, not a day and time.</p> <p>Seemed like I need to search for any date that falls on that day, so logically: <code>ADATE &gt;= user_day_start &amp;&amp; ADATE &lt;= user_day_end</code></p> <p>Now there is an array of dates in the metadata so I really want it to match if ANY of those dates fall within the user_date day.</p> <p>Be nice if this worked:</p> <pre><code>NSDate * date = [NSDate dateWithNaturalLanguageString: inWhenString]; NSDate * endDate = [NSDate dateWithTimeInterval: 86400 sinceDate: date]; predicate = [NSPredicate predicateWithFormat: @"kMDItemAccessedDates between {%@, %@}", date, endDate]; </code></pre> <p>but that throws an exception:</p> <pre><code>Exception detected while handling key input. 2013-01-16 03:12:57.517 Chatty[96974:403] Unknown type of NSComparisonPredicate given to NSMetadataQuery (kMDItemAccessedDates BETWEEN {CAST(379540800.000000, "NSDate"), CAST(379627200.000000, "NSDate")}) </code></pre> <p>and seems like it might only work for a single date rather than an array. Adding the "ANY" to the format string like so: <code>"ANY kMDItemAccessedDates between {%@, %@}"</code> ... didn't help, still throwing an exception.</p> <p>Tried this and it's not throwing an exception, but isn't really working either:</p> <pre><code>NSPredicate *p1 = [NSPredicate predicateWithFormat: @"kMDItemAccessedDates &gt;= %@", date ]; NSPredicate *p2 = [NSPredicate predicateWithFormat: @"kMDItemAccessedDates &lt;= %@", endDate ]; predicate = [NSCompoundPredicate andPredicateWithSubpredicates: [NSArray arrayWithObjects: p1, p2, nil]]; </code></pre> <p>Wondering if anyone has any ideas on how to make this work... or knows that it's impossible (like have to get them all and then do the filter in memory or something; really hope that's not the case - it could be a long list!).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload