ANN: Some routines for Spotlight

Andre Garzia soapdog at mac.com
Sun Aug 21 13:53:09 EDT 2005


Folks,

Since I started using MacOS X Tiger, I became very fond of Spotlight  
since it reminds me of BFS.

Spotlight has support for very advanced queries using nested boolean  
logic and cool metadata filters but those features are not exposed in  
the normal spotlight ui. To access those functions you need to code  
your query using Objective-C or use command line tools.

By the use of shell() command we can harvest most of the spotlight  
power by calling commandline tools, the spotlight command line tools  
start with md (from metadata).

More can be learned from the Ars Technica Tiger review at: http:// 
arstechnica.com/reviews/os/macosx-10.4.ars/9

So, I took some time to create some functions that we can use to  
harvest those powers and they are below:

function mdfind pQuery
   -- This one find matching files based on a given query.
   return shell("mdfind" && pQuery)
end mdfind

function mdls pQuery
   -- Returns a chunk with all Metadata info for a given file.
   return shell("mdls" && pQuery)
end mdls

function MetadataToArray pQuery
   -- Takes the metadata chunk and turns it into an Array.
   -- This is easier to access than the chunk.
   -- There are some extra steps to erase extra spaces.
   local tTempA, tReturnValA
   put mdls(pQuery) into tTempA
   split tTempA by cr and "="
   repeat for each line tKey in the keys of tTempA
     get offset(" ", tKey)
     get char 1 to (it - 1) of tKey
     put tTempA[tKey] into tReturnValA[it]
   end repeat
   return tReturnValA
end MetadataToArray

function mdimport pFile
   -- forces reindexing on a given file, use it after you change its  
metadata.
   -- we all know that the kernel hooks should make this useless but...
   return shell("mdimport" && pFile)
end mdimport



Have fun friends
andre


More information about the use-livecode mailing list