{"id":110,"date":"2006-01-05T10:51:21","date_gmt":"2006-01-05T18:51:21","guid":{"rendered":"http:\/\/www.corbinstreehouse.com\/blog\/?p=110"},"modified":"2018-10-31T10:15:16","modified_gmt":"2018-10-31T17:15:16","slug":"cocoa-using-nspredicate-and-nsmetadataquery","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2006\/01\/cocoa-using-nspredicate-and-nsmetadataquery\/","title":{"rendered":"Cocoa: Using NSPredicate and NSMetadataQuery"},"content":{"rendered":"<p id=\"top\" \/>Hi Apple Cocoa developers. Here are some more tips and tricks for Cocoa development.<\/p>\n<p>On Tiger, there is a new class called NSMetadataQuery that allows you to do some cool Spotlight searches. See the \u00e2\u20ac\u0153Spotlighter\u00e2\u20ac\u009d demo application included in \/Developer\/Examples\/AppKit\/Spotlighter.<\/p>\n<p>Creating a predicate to get the search results that you want can be tricky, but alas, there is an easy way!<\/p>\n<p>First, do a search inside of Finder that has the criteria you want. For example, below is a search for all images recently created:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/SpotlightSearchExampleInFinder.png\" height=\"430\" width=\"386\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Spotlightsearchexampleinfinder\" \/><\/p>\n<p>Hit the save button and save it to your desktop. You can then show the info that Saved Search Query inside of Finder and see the Query it is using:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/SEarchQueryExample.png\" height=\"310\" width=\"258\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Searchqueryexample\" \/><\/p>\n<p>You can also drag and drop it onto TextEdit to open up the file (it is just a simple XML plist). You can then extract the query via copy and paste:<\/p>\n<p>(kMDItemContentTypeTree = &#8216;public.image&#8217;) &amp;&amp; (kMDItemFSCreationDate &gt;= $time.today(-7)) &amp;&amp; (kMDItemContentType != com.apple.mail.emlx) &amp;&amp; (kMDItemContentType != public.vcard)<\/p>\n<p>So, you want to test this query be executing it from the command line. You should be able to do an \u00e2\u20ac\u0153mdfind\u00e2\u20ac\u009d and paste it after it, however, that won&#8217;t work! You must escape the $ with a slash since the shell interprets the slash as a file input.<\/p>\n<p>You also have to quote the entire thing, as follows:<\/p>\n<pre>mdfind \u00e2\u20ac\u0153(kMDItemContentTypeTree = 'public.image') &&\n    (kMDItemFSCreationDate >= \\$time.today(-7)) &&\n    (kMDItemContentType != com.apple.mail.emlx) &&\n    (kMDItemContentType != public.vcard)\u00e2\u20ac\u009d<\/pre>\n<p>And, thus you can use it from a command prompt:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/CommandPromptQuerymdfind.png\" height=\"131\" width=\"545\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Commandpromptquerymdfind\" \/><br \/>\n(sorry for the ugly wrapping).<\/p>\n<p>Okay &#8212; so, one would think they can paste this into a predicate and just use it as-is, right?<\/p>\n<p>Well, no! The following would normally work:<\/p>\n<p>[<b>NSPredicate predicateWithFormat<\/b>:@\u00e2\u20ac\u0153kMDItemContentTypeTree = &#8216;public.image&#8217; &#038;&#038; (kMDItemFSCreationDate >= $time.today(-7)) &#038;&#038; kMDItemContentType != &#8216;com.apple.mail.emlx&#8217;\u00e2\u20ac\u009d] ;<\/p>\n<p>But a small bug prevents it from actually parsing the text due to the $time reference (it might even crash!). You <b>must<\/b> single quote the $time part to get it to work:<\/p>\n<p>[NSPredicate predicateWithFormat:@\u00e2\u20ac\u0153kMDItemContentTypeTree = &#8216;public.image&#8217; &#038;&#038; (kMDItemFSCreationDate >= <b>&#8216;$time.today(-7)&#8217;<\/b>) &#038;&#038; kMDItemContentType != &#8216;com.apple.mail.emlx&#8217;\u00e2\u20ac\u009d] ;<\/p>\n<p>Then, it should all be okay!<\/p>\n<p>Have fun. Happy programming. Long live MacOS.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi Apple Cocoa developers. Here are some more tips and tricks for Cocoa development. On Tiger, there is a new class called NSMetadataQuery that allows you to do some cool Spotlight searches. See the \u00e2\u20ac\u0153Spotlighter\u00e2\u20ac\u009d&#8230; <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2006\/01\/cocoa-using-nspredicate-and-nsmetadataquery\/\">[read more]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[4,6,1],"tags":[63,130,72],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-apple","category-cocoa","category-general","tag-cocoa","tag-objc","tag-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/comments?post=110"}],"version-history":[{"count":1,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":5328,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/110\/revisions\/5328"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}