toxicsoftware.com

RANDOMIZE USR 0

toxicsoftware.com header image 2

mdfind2

January 5th, 2006 · Comments · Default

I’m clearing out some of my old code and found this little gem. mdfind2 is a replacement for Apple’s mdfind command line tool. This version adds one feature that mdfind doesn’t have, you can specify the –xml switch on the command-line to output the results (including full attribute values) in XML.

Here’s an example of output (or just one file, normally the output is a lot longer):

<spotlight>
    <query>kMDItemTextContent == "xyzzy"</query>
    <results>
        <item>
            <name>NSString_PathExtensions.h</name>
            <link>file://localhost/Volumes/Shared/Users/Shared/Developer/Products/Debug/ToxicFoundation.framework/Versions/A/Headers/NSString_PathExtensions.h</link>
            <attributes>
                <attribute key="kMDItemFSOwnerUserID">501</attribute>
                <attribute key="kMDItemFSOwnerGroupID">501</attribute>
                <attribute key="kMDItemFSTypeCode">0</attribute>
                <attribute key="kMDItemFSIsExtensionHidden">0</attribute>
                <attribute key="kMDItemID">322662</attribute>
                <attribute key="kMDItemFSSize">427</attribute>
                <attribute key="kMDItemFSCreationDate">2005-12-19 18:57:40 -0500</attribute>
                <attribute key="kMDItemFSName">NSString_PathExtensions.h</attribute>
                <attribute key="kMDItemFSContentChangeDate">2005-12-19 18:57:40 -0500</attribute>
                <attribute key="kMDItemFSCreatorCode">0</attribute>
                <attribute key="kMDItemFSLabel">0</attribute>
                <attribute key="kMDItemFSInvisible">0</attribute>
                <attribute key="kMDItemFSNodeCount">0</attribute>
                <attribute key="kMDItemAttributeChangeDate">2005-12-21 11:36:14 -0500</attribute>
                <attribute key="kMDItemFSFinderFlags">0</attribute>
                <attribute key="kMDItemUsedDates">2005-12-19 18:57:40 -0500</attribute>
                <attribute key="kMDItemLastUsedDate">2005-12-19 18:57:40 -0500</attribute>
                <attribute key="kMDItemKind">C Header Source File</attribute>
                <attribute key="kMDItemDisplayName">NSString_PathExtensions.h</attribute>
                <attribute key="kMDItemContentType">public.c-header</attribute>
                <attribute key="kMDItemContentTypeTree">public.c-header public.source-code public.plain-text public.text public.data public.item com.apple.cocoa.path com.apple.cocoa.string public.content</attribute>
                <attribute key="kMDItemContentCreationDate">2005-12-19 18:57:40 -0500</attribute>
                <attribute key="kMDItemContentModificationDate">2005-12-19 18:57:40 -0500</attribute>
            </attributes>
        </item>
    </results>
</spotlight>

Source here: http://toxic-public.googlecode.com/svn/tags/BlogTag_20070927_729/Spotlight/mdfind2/ Binary here: http://toxic-public.googlecode.com/svn/tags/BlogTag_20070927_729/Spotlight/mdfind2/Projects/Output/mdfind2.zip

  • Update: Just found an XSLT file that can used to transform the output of mdfind2 to an atom file: http://toxic-public.googlecode.com/svn/tags/BlogTag_20070927_729/Projects/Spotlight/mdfind2/SpotlightToAtom.xsl - and now of course I’ll need to work out how to get this working in NetNewsWire
  • Update: Well that didn’t take long! I can now perform spotlight queries in NetNewsWire:

    1. Download mdfind2 and the SpotlightToAtom.xsl files.
    2. Create a shell script with containing:

Example:

#!/bin/sh  
~/Products/Debug/mdfind2 --xml $* | xsltproc "$HOME/Library/Application Support/mdfind2/SpotlightToAtom.xsl" -

Fix the paths to mdfind2 and SpotlightToAtom.xsl to suit your environment. 3. Create a new script special subscription in NetNewsWire and configure it to point to the script you just created. Set the parameters of the script to the Spotlight query you want executed…SpotlightNNWSpecialSubscription.png 4. Execute the query (or refresh the feed) and NetNewsWire should display the results of the query: image

Tags:

Viewing 3 Comments

    • ^
    • v
    Hm. I'm assuming I'd have to build the binary myself to get it to work properly? Or can the other files with hardcoded paths in the binary be made available too.. not all that savvy here.

    ./mdfind2
    ZeroLink: could not load .o file: /Users/schwa/Developer/Intermediates/mdfind2.build/Debug/mdfind2.build/Objects-normal/ppc/mdfind2.ob
    ZeroLink: could not load .o file: /Users/schwa/Developer/Intermediates/mdfind2.build/Debug/mdfind2.build/Objects-normal/ppc/CMyController.ob
    ZeroLink: could not load .o file: /Users/schwa/Developer/Intermediates/mdfind2.build/Debug/mdfind2.build/Objects-normal/ppc/NSMetadataItem_XMLExtensions.ob
    Bus error
    • ^
    • v
    Yep. I made the classic mistake of shipping the Debug build not the Release build. The zip file now contains the release build binary.
    • ^
    • v
    So, and what is about an XML content importer plugin to Spotlight?

    It will be useful when we could access XML tags and attributes in content of the file directly in search query and results!

    <name>John</name>
    <birthDay>09/10/1971</birthDay>

    Search query should be "... birthDay == 09/10/1971", and result like an XML node tree: kMDItemFSXMLTag.name=John
    kMDItemFSXMLTag.birthDay=09/10/1971
 
close Reblog this comment
blog comments powered by Disqus