Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.4: Phrase searching using Spotlight and Python' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Phrase searching using Spotlight and Python
Authored by: larryy on Jun 15, '07 05:15:28PM

I think a few people are confused about what "phrase" means in this context. Spotlight will happily find documents with multiple words (including if they are separated by + or & instead of space, though they do seem to change the results slightly). SpotInside.app behaves the same as Spotlight, except it has a document preview pane.

What is needed is a simple way to find only those documents that contain a "phrase"--a set of contiguous words in exactly the given sequence. (What Spotlight finds is all documents that contain that set of words in any sequence anywhere in the document.)

The Python script seems to do this, but is quite slow and normally works over the entire disk only. There is a -o directory option to limit the search to a particular directory, but it doesn't appear to work, instead searching at least the directory containing the specified directory. (If that proves to be all that is wrong, then specifying a directory inside the directory you want to search might be a workaround.)

The Perl one-liner seems to work correctly when used as shown here, and it seems fairly quick, and the updated version nicely allows you to specify the starting directory. However, when I convert it to a bash function, it ceases to work for some reason, sadly, but that's probably me. Here's what I did, in case anyone can spot the error (it's a direct copy and paste, except for sticking it in the bash function and replacing the specific directory and search string with $*):

search () { command perl -e '($f,@A) = map { quotemeta } @ARGV; open FH, "mdfind -onlyin $f \"@A\" |"; $A = join ".{1,20}",@A; while ($r=<FH>) { chomp ($r) ; $s=quotemeta($r); @x = grep {m/$A/} `mdimport -nfd2 $s \&> /tmp/crap ; cat /tmp/crap`; print "$r\n" if @x>0} ' $* ; }

To bad this has to use an intermediate file, as it would probably be quite fast otherwise.



[ Reply to This | # ]