[MacRuby-devel] Trying to use spotlight programmatically

Kaelin Colclasure kaelin at apple.com
Tue Feb 8 11:49:39 PST 2011


Martin,

The FinderFilesOnly stuff is not really relevant to other applications using Spotlight. You should not need to do anything special to find email results. Try, for example, using the mdfind command from Terminal:

$ mdfind -literal 'kMDItemAuthorEmailAddresses == *'

If your query is not returning any results, there is probably something else going on. Here is a quick example using macirb:
---8<---
[kaelin at melete trunk]$ macirb
irb(main):001:0> framework 'CoreServices'
=> true
irb(main):002:0> query = MDQueryCreate(KCFAllocatorDefault, "kMDItemAuthorEmailAddresses == *", nil, nil)
=> #<__NSCFType:0x4001149e0>
irb(main):003:0> MDQueryExecute(query, KMDQuerySynchronous)
=> true
irb(main):004:0> MDQueryGetResultCount(query)
=> 66883
irb(main):005:0> 
--->8---

HTH,

-- Kaelin


On Feb 7, 2011, at 10:25 AM, Martin Hawkins wrote:

> This is really beyond the remit of this group, but I thought I'd try
> here first as you guys are so helpful!
> 
> I'm trying to use spotlight programmatically.
> I've downloaded the sample apps and used ideas from those - the code
> works fine in MacRuby.
> 
> Where I'm coming unstuck is when I try to search mail. When creating
> an ordinary spotlight search, you can add a criteria 'Spotlight items
> are included'.
> This is required when searching on something like Author Email
> addresses (which translates to kMDItemAuthorEmailAddresses), because
> without it, no hits are recorded. I mention
> kMDItemAuthorEmailAddresses because as the attribute exists, the
> expectation that a query can be parsed using it is set.
> If you create two searches, one with and one without the spotlight
> requirements line, save the searches and compare them (they are
> in .plist format), the one with the 'Spotlight items are included'
> criteria has the key FinderFilesOnly set to false:
> 
> <key>FinderFilesOnly</key>
> 		<false/>
> 
> and the following extra criteria in FXCriteriaSlices:
> <dict>
>  <key>criteria</key>
>  <array>
>    <string>com_apple_SearchSpotlightItemsAttribute</string>
>    <integer>1230</integer>
>  </array>
>  <key>displayValues</key>
>  <array>
>    <string>SEARCH_SPOTLIGHT_ITEMS</string>
>    <string>YES</string>
>  </array>
>  <key>rowType</key>
>  <integer>0</integer>
>  <key>subrows</key>
>  <array/>
> </dict>
> 
> I can't see documentation that explains how to achieve this
> programmatically; my programmatic search, like the ordinary spotlight
> search without the 'Spotlight items are included', yields 0 hits at
> the moment.
> Any help very welcome!
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel



More information about the MacRuby-devel mailing list