Herewith two hideously ugly little shell scripts for use when Spotlight refuses to search your mail. Spotlight is a flawed v1.0 implementation of a really good idea and will, I’m sure, be debugged in a near-future release. [Update: The LazyWeb is educating me... these are moving targets.]

My problem is that whereas Mail.app will search my To/From/Subject lines (slowly, and with a really irritating GUI), the “Entire Message” option just doesn’t work, it returns instantly with no results. Yes, I’ve read the hints about making Spotlight re-index, but it just flatly refuses to work for me. Mind you, I have a lot of email, but still, it should at least try.

It turns out I had never really figured out the -print0 and -0 idioms that a lot of the shell-command stalwarts now have. Thanks to Malcolm Tredinnick for raising my consciousness.

This lives in $HOME/bin under the name mailgrep:

#!/bin/sh
find $HOME/Library/Mail/IMAP* -name '*.emlx' -print0 | \
  xargs -0 fgrep -i $@

Isn’t xargs a funny command? I’ve discovered that it’s nearly impossible to describe what does, and then why what it does is necessary, but there are just a whole bunch of places where you’d be lost without it.

This lives in $HOME/bin/mailview:

#!/bin/sh
find $HOME/Library/Mail/IMAP* -name '*.emlx'  -print0 | \
  xargs -0 fgrep -i -l -Z $@ | \
  xargs -0 open

The first cut of this dodged xargs and used an incredibly-inefficient and slow chain of -exec arguments to open the files one at a time with view (aka vim), to work around a well-known vim misfeature; it complained about the input not being a terminal and left my Terminal.app keystrokes borked.

But Malcolm, confirming my belief in the broken-ness of vim, said “Oh, *that* ‘view’. I thought it was some sexy Mac ‘view my email’ app”. D’oh, of course; the magic OS X open command does just the right thing. Erm, you might want to run mailgrep before you run mailview; I’m not sure what would happen if you asked OS X to open three or four thousand email messages at once.


author · Dad · software · colophon · rights
picture of the day
April 21, 2006
· Technology (87 fragments)
· · Mac OS X (119 fragments)
· · · Gripes (22 more)

By .

The opinions expressed here
are my own, and no other party
necessarily agrees with them.

A full disclosure of my
professional interests is
on the author page.