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

A script to create HTML tables in Mail Apps
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 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 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.]
  • Currently 2.20 / 5
  You rated: 1 / 5 (5 votes cast)

A script to create HTML tables in Mail | 10 comments | Create New Account
Click here to return to the 'A script to create HTML tables in Mail' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to create HTML tables in Mail
Authored by: Pedro Estarque on May 14, '07 09:46:25AM

Geeklog must have eaten the spaces in the cells, but they are essential if you want to align them. So it should be:

| MacBook | __White___ | USD$ 1,099 |
| MacBook | ___Black___| USD$ 1,499 |
| iPod ___| TouchScreen | ___unknown |

[ Reply to This | # ]
A script to create HTML tables in Mail
Authored by: Dieringer on May 14, '07 10:15:30AM

I've used TextEdit and MS Word to create tables and paste into Mail. Ditto for bullet lists and finer control over formatting.

[ Reply to This | # ]
It's not Geeklog - blame HTML
Authored by: rv8 on May 14, '07 04:55:17PM
Geeklog didn't eat your spaces. If you look at the page source, they are still there. But, multiple spaces in html are collapsed into one space when displayed in the browser.

Kevin Horton

[ Reply to This | # ]

It's not Geeklog - blame HTML
Authored by: Pedro Estarque on May 15, '07 07:29:31AM

yes it did, I used the non-breaking space ( &nbsp ) and the preview showed it correctly.

[ Reply to This | # ]
A script to create HTML tables in Mail
Authored by: kaih on May 14, '07 04:16:33PM

An easier (and WYSIWYG) way to put tables, and other rich text formatting in Mail is to use TextEdit.

In TextEdit, make a new document and make sure it's Rich Text (the default). You can tell it's rich text by the toolbars at the top of the window.

Then go to Format -> Text -> Table and the Table palette appears, from here you can make tables to your heart's content - then simply cut-n-paste into a new Mail message and you're right to go...


[ Reply to This | # ]
A script to create HTML tables in Mail
Authored by: rjetton on May 14, '07 06:33:12PM

If all you need is a simple table, try switching to a fixed pitch font like Courier for that material.

[ Reply to This | # ]
A script to create HTML tables in Mail
Authored by: S Barman on May 14, '07 08:31:13PM
If you add the following
exit 0
as the last line of the script, the error message Rob wrote about will disappear.

[ Reply to This | # ]
A script to create HTML tables in Mail
Authored by: lar3ry on May 15, '07 11:49:22AM
Very interesting technique. I never considered this, and wondered why was so obstinate about the limited RTF it will accept as input.

I think that any "text to html" shell utility/script would do. For instance, I use a text formatter I wrote called "" (written in Perl) to "standardize" text that I write, and to convert from the shorthand text that I use for writing to various formats such as XML and HTML for presentation.

This hint has the start of something very useful...!

So, now my next move is to create a single AppleScript to automate all of this. A lot of the following is already in the script from this hint, but it should be packaged a bit more concisely:

  • Copy text from
  • Feed it to a special shell script that does the following:
    • Use pbpaste to get the text
    • Use iconv to convert to ISO-8859-1
    • Pass it through " -H" to generate HTML (or whatever text to HTML utility that is desired)
    • Use textutil to convert the HTML to RTF
    • Use pbcopy to store the RTF text into the copy/paste buffer
  • Tell to replace the contents of the message with the paste buffer.

Finally, I can then bind this AppleScript to a keystroke using the free Mail Act-On, so all I need to do is type my text in the short hand that I use for writing, and then press my special key to get it nicely formatted within!

[ Reply to This | # ]

A script to create HTML tables in Mail
Authored by: Fairly on May 17, '07 05:29:28PM

1. Make the table in a format of your own choosing and then zip it up and attach it.

2. You and your friends always use fixed pitch fonts and you put in the table as plain text.

[ Reply to This | # ]
Correct txt2tags URL
Authored by: aureliojargas on May 18, '07 06:17:31PM
The URL for the txt2tags site is giving 404. The correct is

[ Reply to This | # ]