You can convert an attributed string to HTML. The result is similar to what you get when you save a file to .html format in TextEdit.
In this example the attributed string is coming from the clipboard, and is then converted to data. The key to producing HTML is the documentAttributes: parameter – you have to specify NSHTMLTextDocumentType as the document type used when creating the data.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
-- classes, constants, and enums used
property NSUTF8StringEncoding : a reference to 4
property NSPasteboard : a reference to current application's NSPasteboard
property NSPasteboardTypeRTF : a reference to current application's NSPasteboardTypeRTF
property NSAttributedString : a reference to current application's NSAttributedString
property NSString : a reference to current application's NSString
property NSHTMLTextDocumentType : a reference to current application's NSHTMLTextDocumentType
set pb to NSPasteboard's generalPasteboard() -- get pasteboard
set theData to pb's dataForType:(NSPasteboardTypeRTF) -- get rtfd data off pasteboard
if theData = missing value then error "No rtf data found on clipboard"
-- make into attributed string
set theAttString to NSAttributedString's alloc()'s initWithRTF:theData documentAttributes:(missing value)
set {htmlData, theError} to theAttString's dataFromRange:{0, theAttString's |length|()} documentAttributes:{DocumentType:NSHTMLTextDocumentType} |error|:(reference)
if htmlData = missing value then error theError's localizedDescription() as text
set theString to (NSString's alloc()'s initWithData:htmlData encoding:NSUTF8StringEncoding) as text
You could also save the data directly to a file, using one of the writeToFile:
methods.
Just as when saving from TextEdit, this can produce some very verbose HTML. You can reduce the complexity by specifying a list of elements to be skipped. Again, the documentAttributes: parameter is where you specify this. Here is an example that strips out many of the standard elements:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
-- classes, constants, and enums used
property NSDocumentTypeDocumentAttribute : a reference to current application's NSDocumentTypeDocumentAttribute
property NSExcludedElementsDocumentAttribute : a reference to current application's NSExcludedElementsDocumentAttribute
property NSUTF8StringEncoding : a reference to 4
property NSPasteboard : a reference to current application's NSPasteboard
property NSPasteboardTypeRTF : a reference to current application's NSPasteboardTypeRTF
property NSAttributedString : a reference to current application's NSAttributedString
property NSString : a reference to current application's NSString
property NSHTMLTextDocumentType : a reference to current application's NSHTMLTextDocumentType
set pb to NSPasteboard's generalPasteboard() -- get pasteboard
set theData to pb's dataForType:(NSPasteboardTypeRTF) -- get rtfd data off pasteboard
if theData = missing value then error "No rtf data found on clipboard"
-- make into attributed string
set theAttString to NSAttributedString's alloc()'s initWithRTF:theData documentAttributes:(missing value)
set elementsToSkip to {"doctype", "html", "body", "xml", "style", "p", "font", "head", "span"} -- ammend to suite
set theDict to current application's NSDictionary's dictionaryWithObjects:{current application's NSHTMLTextDocumentType, elementsToSkip} forKeys:{NSDocumentTypeDocumentAttribute, NSExcludedElementsDocumentAttribute}
set {htmlData, theError} to theAttString's dataFromRange:{0, theAttString's |length|()} documentAttributes:theDict |error|:(reference)
if htmlData = missing value then error theError's localizedDescription() as text
set theString to (NSString's alloc()'s initWithData:htmlData encoding:NSUTF8StringEncoding) as text