[MacRuby-devel] Trying to use spotlight programmatically

Franco Rondini rondinif at yahoo.it
Tue Mar 1 18:40:26 PST 2011


Hi guys,
I've adopted a pragmatical workaround that insanely works:

	ar  = %x[mdfind -literal 'kMDItemDisplayName == *.pdf && kMDItemFSSize > 100000'].split("\n")

	p "found #{ar.count} pdf > 100.000 bytes"

In this way I can do something like that:

	ar2d = ar.collect {|x| [Digest::MD5.hexdigest(File.read(x)), x]} 
that is, perform an operation for each file found and assign the result to the file path.

I am fully satisfied now, although this does not directly use the CoreServices framework, 
my interest is in understanding more fully how to use MD* interfaces directly in the code MacRuby, so if someone managed to do it I'd love to hear news.

greetings,
Franco Rondini

> 
>   1. 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Tue, 1 Mar 2011 00:12:53 +0100
> From: Franco Rondini <rondinif at yahoo.it>
> To: macruby-devel at lists.macosforge.org
> Subject: Re: [MacRuby-devel] Trying to use spotlight programmatically
> Message-ID: <3964BBA2-BB57-4860-B45B-39B3E1742351 at yahoo.it>
> Content-Type: text/plain; charset="us-ascii"
> 
> Hello , 
> The code posted by Kaelin works great for me, 
> After counting the number of search results now I would like to know the path of files found but to do this I have big difficulty
> 
> I'm trying to figure out how to make it works the MDQueryGetAttributeValueOfResultAtIndex or MDItemCopyAttribute to read out the KMDItemPath or KMDItemFSName but I was a bit confused by the "Pointer" returned and even I get a # Illegal instruction as you can see in the following code:
> 
> framework "CoreServices"
> #=> true
> query = MDQueryCreate(KCFAllocatorDefault, "kMDItemDisplayName = '*.pdf'", [KMDItemFSName,KMDItemPath] , nil)
> #=> #<__NSCFType:0x2005d63a0>
> MDQueryExecute(query, KMDQuerySynchronous)
> #=> true
> # item = MDQueryGetResultAtIndex(query, 1);
> #=> #<Pointer:0x20052dc80>
> count = MDQueryGetResultCount(query)
> p "found #{count} documents" #=> 981
> a = MDQueryGetAttributeValueOfResultAtIndex(query, KMDItemFSName, 2)
> p a #<Pointer:0x20059aee0>   ..not a NSString
> p a[0] #0 
> p a[1] #4
> 
> item_ref = MDQueryGetResultAtIndex(query, 1)
> p item_ref  #<Pointer:0x200582460>
> p item_ref.class #Pointer ..not a MDItemRef
> path = MDItemCopyAttribute( item_ref, KMDItemPath) #Illegal instruction
> p path
> 
> I've found an older post of 2009 titled: "[MacRuby-devel] spotlight - MDItemRef" where Laurent said: "I'm afraid the MD* APIs haven't been covered by BridgeSupport yet.."
> but because of i can see the MDQueryGetResultCount working I hope that there is a way to enumerate the results of the query to get the attributes ( path especially )
> I would be very happy if anyone has any idea, 
> Thanks
> Franco 
> 
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20110301/7e87e545/attachment-0001.html>



More information about the MacRuby-devel mailing list