I know HTML email is evil and all, but sometimes I need to send some formated text and tab/space alignment just doesn't cut it -- each mail client renders the message differently (different fonts, kerning etc.). Most of the time I want to send HTML tables, but Mail.app doesn't let you to insert them AFAIK, and sending a PDF seems like overkill. So I usually code it in HTML and use the "Mail Contents of this Page" trick, but that's a little annoying.
So I wrote this bash script that transform text to HTML tables, and inserts it back into the message. The message must be in rich text format (Format » Make Rich Text), and be the frontmost window for it to work. The syntax is loosely based on txt2tags. The script accepts input like this:
| MacBook | White | USD$ 1,099 |
| MacBook | Black | USD$ 1,499 |
| iPod | TouchScreen | unknown |
There must be a blank line before and after each table, and a space after each pipe character (|). Cells can be aligned with spaces like the example above, and complete tables can be aligned using some special characters.
Here's how to align tables to the left, center, or right:
[[
| This | table | is aligned | to the left |
[]
| This | table | is aligned | to the center |
]]
| This | table | is aligned | to the right |
The script converts the text to HTML, changes the character set, and then converts it to RTF. The trick is that if you cat sometext.rtf | pbcopy, you can paste it in Rich Text applications like TextEdit and Mail. If you cat mypage.html | pbcopy, you get the code pasted instead. I tried to make this a service with ThisService, but it didn't work due to the bug in Mail.app described on the ThisService developer's page. But you can run it with your favorite launcher (QuickSilver, Butler, etc.). Any previous formatting, such as bold, text alignment, etc. will be lost, so make your tables first, then format the rest.
[robg adds: Here's how to use the script: copy and paste the text into a new blank text file in your editor of choice. Save it to somewhere like your user's bin folder, or any other permanent location (ideally one already on your $PATH). In Terminal, cd to that directory, and make your script executable: chmod 755 txt2tbl (or whatever you called it). In Mail, type your new message, making sure there are blank lines above and below the table, and that you're composing in rich text mode. Then switch to Terminal and run the script (i.e. ./txt2tbl if you're still in the same directory). The script will activate Mail and attempt to convert the frontmost message.
On my machine, the same execution error (Can't get end. (-1728)) occurs twice every time I run the script -- but it still seems to run just fine.]
Mac OS X Hints
http://hints.macworld.com/article.php?story=20070510153704248