create a HTML table

I got tired of trying to find my favorite html table generator; It kept getting moved around in google search. Additionally, I didn’t like the entry for the cells. Often time, a cell at a time had to be copied. I don’t find that efficient.

So, I wrote my own html table generator.

Caveats:

  • Not much error checking
  • Top row can have different bgrolor, font, and alignment
  • It is written so the is not first column header; it’s just a bunch of rows
  • No warranty implied or expressed
  • Property list at top to control setable values
  • Can put a caption at top, bottom of table or have none
  • Straight table block, not wrapped in body tags

Here it is

property AllTableBorder : 1.0
property AllCellPadding : 5.0
property AllCellSpacing : 0.0
property AllBorderColor : "000000"

-- next two field property values left, center, right
property TitleAlignment : "center"
property CellAlignment : "left"

property TitleBGColor : "bebebe"
property CellBGColor : "FFFFFF"

property Caption : "Put caption here"
-- three setting for caption side-- top, bottom, none
property CaptionSide : "none"

--width of the tqble compared to container n percentage
property WidthPercent : "50%"

property TitleFont : "Times"
property CellFont : "Times"


-- this is where the cell data is inserted
-- need more rows, copy and paste a line
-- need more cells, add more elements to each array 
set ContentOfTable to {¬
	{"h1", "h2", "h3"}, ¬
	{"r1c1", "r1c2", "r1c3"}, ¬
	{"r2c1", "r2c2", "r2c3"}, ¬
	{"r3c1", "r3c2", "r3c3"}, ¬
	{"r4c1", "r4c2", "r4c3"} ¬
		}



-- get the file ready for use
set fileName to ((path to desktop folder) as text) & "table.html"
set thefullpath to POSIX path of fileName
do shell script "touch \"" & thefullpath & "\""

set the_file to alias (fileName) as alias

--if you happen to leave the file open, uncomment this next line once to get it back to the proper state
--close access the_file

open for access the_file with write permission
set eof of the_file to 0


--write the table styles
write "<table border=\"" & AllTableBorder & "\" cellpadding=\"" & AllCellPadding & "\" cellspacing=\"" & AllCellSpacing & "\" bordercolor=\"" & AllBorderColor & "\" width=" & WidthPercent & ">
" to the_file starting at eof as string

--take care of caption if needed
if CaptionSide is not "none" then
	write "<caption style=\"caption-side:" & CaptionSide & "\" >" & Caption & "</caption>
" to the_file starting at eof as string
end if

set firstitem to true

--take care of every row
repeat with thisRow in ContentOfTable
	--specialize the first row
	if firstitem then
		set theBGColor to TitleBGColor
		set theAlignment to TitleAlignment
		set theFont to TitleFont
		set tableStartElement to "<tr"
		set tableEndElement to "</tr>"
	end if
	-- wite out the <tr> block
	write tableStartElement & " bgcolor =\"" & theBGColor & "\">
" to the_file starting at eof as string
	--write on the cell contents
	repeat with thisCell in thisRow
		write "	<td align=\"" & theAlignment & "\" width=\"0\"><span style=\"font:12px " & theFont & "; \"> " & thisCell & "<br /></span></td>
" to the_file starting at eof as string
	end repeat
	
	-- write out the< /tr>
	write tableEndElement & "
" to the_file starting at eof as string
	--get ready for other rows
	if firstitem then
		set firstitem to false
		set theBGColor to CellBGColor
		set theAlignment to CellAlignment
		set theFont to CellFont
		set tableStartElement to "<tr"
		set tableEndElement to "</tr>"
	end if
	
end repeat

--don't forget the </table>, otherwise the rest of the html will me mucked
write "</table>" to the_file starting at eof as string


close access the_file

Hope someone else finds this useful