HTML Styled text in a variable

This should be pretty simple. I have text with some very basic HTML tags.

<p><b>The Tonight Show Starring Jimmy Fallon</b> Milo Ventimiglia; journalist Guy Raz; comic Carmen Lynch. (N) 11:34 p.m. KNBC</p>

I want to convert that to styled text that I can paste into our CMS app with styles intact. (It used to accept the raw HTML, but doesn’t anymore and they’re not planning to fix it).

In SD, the text displays in the Best window perfectly, and if I copy that and paste it, that works.

I can also save the text in an html document, open the document in safari, copy from there and paste it and it works (although when doing that from Chrome it loses all of the bold and headline tags).

Any suggestions?

Where do you have the HTML? In a variable, or on the clipboard?

There are at least two methods to convert HTML code to Rich Text, and put on the Clipboard.

  1. Shell script
  2. ASObjC

Here is my shell script handler I have been using for years:

on pasteHTMLasRTFtoClipboard(pstrHTML)
  
  -- REWRITTEN AS RTF AND COPIED TO THE CLIPBOARD
  set lstrCMD to "echo " & quoted form of pstrHTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
  do shell script lstrCMD
  
end pasteHTMLasRTFtoClipboard

Here is a script that @ShaneStanley wrote in 2018 that puts both Rich Text and plain text on the clipboard:

property ptyScriptName : "Convert HTML to RTF and Set Clipboard"
property ptyScriptVer : "1.0"
property ptyScriptDate : "2018-05-28"
property ptyScriptAuthor : "ShaneStanley"

(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:
  • How Do I Set Clipboard (Pasteboard) to Both Rich Text (RTF) and Plain Text?
  
REF:  The following were used in some way in the writing of this script.

  1.  2018-05-28, ShaneStanley, Late Night Software Ltd.
      How Do I Set Clipboard (Pasteboard) to Both Rich Text (RTF) and Plain Text?
      http://forum.latenightsw.com/t/how-do-i-set-clipboard-pasteboard-to-both-rich-text-rtf-and-plain-text/1189/5

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
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 NSString : a reference to current application's NSString
property NSRTFTextDocumentType : a reference to current application's NSRTFTextDocumentType
property NSPasteboardTypeRTF : a reference to current application's NSPasteboardTypeRTF
property NSPasteboardTypeString : a reference to current application's NSPasteboardTypeString
property NSDictionary : a reference to current application's NSDictionary
property NSAttributedString : a reference to current application's NSAttributedString
property |NSURL| : a reference to current application's |NSURL|

set someHTML to "Click <b>here</b>"
-- convert to data
set htmlString to NSString's stringWithString:someHTML
set htmlData to htmlString's dataUsingEncoding:NSUTF8StringEncoding
-- make attributed string
set attString to NSAttributedString's alloc()'s initWithHTML:htmlData documentAttributes:(missing value)
-- need it in RTF data form for clipboard
set rtfData to attString's RTFFromRange:{0, attString's |length|()} documentAttributes:{DocumentType:NSRTFTextDocumentType}
set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
pb's clearContents()
-- set both types for the first object on the clipboard
pb's setData:rtfData forType:NSPasteboardTypeRTF

FWIW, it turns out what I posted then can be simplified a bit:

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 NSString : a reference to current application's NSString
property NSAttributedString : a reference to current application's NSAttributedString

set someHTML to "Click <b>here</b>"
-- convert to data
set htmlString to NSString's stringWithString:someHTML
set htmlData to htmlString's dataUsingEncoding:NSUTF8StringEncoding
-- make attributed string
set attString to NSAttributedString's alloc()'s initWithHTML:htmlData documentAttributes:(missing value)
set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
pb's clearContents()
pb's writeObjects:{attString}

And if the HTML is on the clipboard rather than in a variable, it’s simpler again:

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 NSPasteboardTypeString : a reference to current application's NSPasteboardTypeString
property NSAttributedString : a reference to current application's NSAttributedString

-- put some HTML on for testing
set the clipboard to "Click <b>here</b>"

set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
set htmlData to pb's dataForType:NSPasteboardTypeString -- get data off pasteboard
-- make attributed string
set attString to NSAttributedString's alloc()'s initWithHTML:htmlData documentAttributes:(missing value)
pb's clearContents()
pb's writeObjects:{attString}
1 Like

Thanks, guys, that worked like a charm!

There is one issue with the script. It doesn’t handle diacriticals well.

é becomes é

Any suggestions? (I’m using the last version Shane posted)

They need to be encoded in your HTML. You may be able to get away with something this simple:

use script "RegexAndStuffLib" version "1.0.6"

-- put some HTML on for testing
set the clipboard to (hex encode "Click <b>hére</b>")