property wrapInASDivTag : true

property includesHeader : true

property includesFooter : true

on run {}

     tell application "Script Editor"

         set doc to document 1

         set tabs to 0

        

         set x to ""

        

         if includesHeader then

             set x to "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">

<html xmlns=\"http://www.w3.org/1999/xhtml\">

<head>

<style>

#Applescript p { line-height: 1em; margin: 0; padding: 0; }

</style>

<title>" & name of doc & "</title>

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />

</head>

<body>"

         end if

        

         set x to x & "<!-- Applescript -->" & return

        

         if wrapInASDivTag then

             set x to x & "<div id=\"Applescript\">" & return

         end if

        

         set l to number of paragraphs in text of doc

         set x to x & "<p>"

         repeat with k from 1 to l

             set j to number of ( every attribute run in paragraph k of text of doc )

             repeat with i from 1 to j

                 set y to attribute run i of paragraph k of doc

                 if y contains "    " then

                     set d to ""

                     repeat with tai from 1 to length of y

                         if character tai of y is "    " then

                             set d to d & "    "

                         else

                             set d to d & character tai of y as string

                         end if

                     end repeat

                     set y to d

                 end if

                 if "<" is in y or ">" is in y then

                     set d to ""

                     repeat with a from 1 to number of characters in y

                         set z to character a in y

                         if z is "<" then

                             set d to d & "<"

                         else if z is ">" then

                             set d to d & ">"

                         else

                             set d to d & z as string

                         end if

                     end repeat

                     set y to d

                 end if

                 set c to my convertColor ( color of attribute run i of paragraph k of doc )

                 if "bold" is in ( font of attribute run i of paragraph k of doc ) then

                     set x to x & "<span style=\"font-weight:bold;color:#" & c & "\">" & y & "</span> "

                 else

                     set x to x & "<span style=\"color:#" & c & "\">" & y & "</span> "

                 end if

             end repeat

             set x to x & "</p>" & return

         end repeat

        

         if wrapInASDivTag then

             set x to x & "</div>" & return

         end if

        

         set x to x & "<!-- Applescript end -->"

        

         if includesFooter then

             set x to x & "</body>" & return & "</html>"

         end if

        

         set fileDest to choose file name

         set theFile to open for access fileDest with write permission

         write x to theFile

         close access theFile

        

     end tell

end run

on convertColor ( c )

     set x to ""

    

     repeat with z from 1 to 3

        

         set i to (( item z of c ) / 256) - 1 as integer

        

         if i = -1 then

             set i to 0

         end if

        

         set j to i / 17 as integer

        

         if j = 15 then

             set x to x & "F"

         else if j = 14 then

             set x to x & "E"

         else if j = 13 then

             set x to x & "D"

         else if j = 12 then

             set x to x & "C"

         else if j = 11 then

             set x to x & "B"

         else if j = 10 then

             set x to x & "A"

         else

             set x to x & j as string

         end if

        

         set j to i mod 16

        

         if j = 15 then

             set x to x & "F"

         else if j = 14 then

             set x to x & "E"

         else if j = 13 then

             set x to x & "D"

         else if j = 12 then

             set x to x & "C"

         else if j = 11 then

             set x to x & "B"

         else if j = 10 then

             set x to x & "A"

         else

             set x to x & j as string

         end if

     end repeat

     return x

end convertColor