Home > Mac | Tech > jsgrep (Mac OS X only!)

jsgrep (Mac OS X only!)

After Mac OS X 10.4, Mac has Spotlight, which is automatic metadata indexing for your data. By using its metadata, you can do grep, but only for JavaScript files. The command called mdfind (Meta Data FIND) does the job for you. I wrote a shell script, not to remember all the tedious options.

#!/bin/sh
/usr/bin/mdfind "kMDItemContentTypeTree == 'public.source-code' && kMDItemFSName == '*.js' && kMDItemTextContent == '$1'"

It’s just verbose, but easy to understand, right? So, what if you want to do the same thing for… Ruby? Just find a ruby script you’re using, or make “test.rb” on your home directory. do “mdls test.rb”, which will probably give you something like follows:

% mdls test.rb
kMDItemContentCreationDate     = 2009-04-XX XX:XX:XX -XXXX
kMDItemContentModificationDate = 2009-04-XX XX:XX:XX -XXXX
kMDItemContentType             = "public.ruby-script"
kMDItemContentTypeTree         = (
    "public.ruby-script",
    "public.shell-script",
    "public.script",
    "public.source-code",
    "public.plain-text",
    "public.text",
    "public.data",
    "public.item",
    "public.content"
)
kMDItemDisplayName             = "test.rb"
kMDItemFSContentChangeDate     = 2009-04-XX XX:XX:XX -XXXX
kMDItemFSCreationDate          = 2009-04-XX XX:XX:XX -XXXX
kMDItemFSCreatorCode           = ""
kMDItemFSFinderFlags           = 0
kMDItemFSHasCustomIcon         = 0
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 0
kMDItemFSIsStationery          = 0
kMDItemFSLabel                 = 0
kMDItemFSName                  = "test.rb"
kMDItemFSNodeCount             = 0
kMDItemFSOwnerGroupID          = 20
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 13
kMDItemFSTypeCode              = ""
kMDItemKind                    = "Ruby Source File"
kMDItemLastUsedDate            = 2009-04-XX XX:XX:XX -XXXX
kMDItemUsedDates               = (
    2009-04-XX XX:XX:XX -XXXX
)

So it should be like…

mdfind "kMDItemContentTypeTree == 'public.ruby-script' && kMDItemFSName == '*.rb' && kMDItemTextContent == 'SEARCH_QUERY'"

mdfind gives you many advantages of metadata search. You should definitely give a shot.

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://blog.nydd.org/2009/04/jsgrep-mac-os-x-only/trackback/
Listed below are links to weblogs that reference
jsgrep (Mac OS X only!) from Vantage Point of Queens

Home > Mac | Tech > jsgrep (Mac OS X only!)

Search
Feeds
Meta
Links
Ads!

Return to page top