2004-07-12

Evolution and grep(1), and Xcode

I wanted to tell Evolution that anything in the big5 charset is junk. (Even if that weren't strictly true, I don't speak any non-European languages, so it may as well be treated as junk for all the good I'll get out of it.) Ever since posting to the ruby-core mailing list, I've been getting a lot of spam from the far east.

Anyway, most of the big5 spam I get is multi-part with no charset in the message header, but charsets in the individual parts. In most mailers, if you say "in headers", they only include the message headers. If you say "in body", they mean the decoded parts, not their headers. Running out of options on Evolution's extensive list (I don't know what the promising-sounding "Regex Match" works on; I certainly couldn't get it to work), I came to "Pipe to Program". Awesome. A quick grep -q later, and I had my crude but effective filter.

Apple are pretty good at this kind of "or there's the whole Unix tool-set if you prefer" approach too; many programs have a directory they'll look in for scripts. Each script gets an entry on a scripts menu that's represented by a little image of a scroll. (You can find the image in /Applications/Mail.app/Contents/Resources/scriptMenu.tiff, though I'm not sure you're supposed to just copy it into your own application. I wish Apple would make it easier to use the same icons. Even better if they'd make them available for cross-platform use, like Sun did with their Java LAF icons, but Mac-only would be a useful step in the right direction.)

To use a script, just select it off the menu. When you do, it's run with data passed in on standard input. What happens to the output is up to the application. Xcode lets you choose between discarding it, replacing the selection, replacing all the text, inserting after the selection, inserting after all the text, putting it in an alert panel or sticking it on the clipboard. You choose which by putting a special comment (such as "%%%PBXOutput=InsertAfterSelection}%%%") in your script. I don't really like this solution, but luckily my applications haven't need this kind of control yet.

Additionally, Xcode also lets you set the selection by inserting "%%%{PBXSelection}%%%" markers in your output, in case you want to control caret positioning yourself, or automatically make a selection.

Xcode also rewrites the script before executing it, replacing various special variables such as "%%%{PBXFilePath}%%%". In my own applications, I've found it more convenient to pass such supplementary information in via environment variables.

I like programs like these. I like to see that the Unix baby isn't being thrown out with the Bourne shell and glass tty bath water.