This has come up a few times recently, so here's a general summary note:
Once you have an HTML or RTF string, textutil does pretty good and easy HTML ⇄ RTF conversion.
Even if the clipboard contains HTML or RTF content, however, inspection through Applescript
the clipboard as record
will reveal it to be in a hex-encoded format, rather than as an expanded and legible string of the kind required by textutil
Unpacking it from Applescript has traditionally required something like:
-- classType: e.g. «class RTF »
on pboardUnpacked(classType)
return (do shell script "osascript -e 'the clipboard as " & classType & ¬
"' | perl -ne 'print chr foreach unpack(\"C*\"," & ¬
"pack(\"H*\",substr($_,11,-3)))' | pbcopy -Prefer txt; pbpaste")
end pboardUnpacked
Alternatively, using the built-in .stringForType() function of NSPasteboard, we could write (in JavaScript for Applications):
ObjC.import('AppKit');
// Types: 'public.rtf', 'public.html' etc
function pboardUnpacked(strType) {
return ObjC.unwrap(
$.NSPasteboard.generalPasteboard.stringForType(
strType
)
)
}
Remember that apps vary in what they put on the clipboard. Safari offers public.rtf but not public.html (it does puts a hex-encoded WebArchive version in the clipboard), whereas after copying from Chrome, you will find public.html contents in the clipboard, but no public.rtf.
Textutil, however, will convert string versions of HTML to RTF (and vice versa)
For example:
pbpaste | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf
UPDATE
For Safari WebArchive clipboard content (plist rather than text), again in JavaScript for Applications:
ObjC.import('AppKit');
// Types: 'com.apple.webarchive' etc
function pboardPlist(strType) {
return ObjC.deepUnwrap(
$.NSPasteboard.generalPasteboard.propertyListForType(
strType
)
)
}
pboardPlist('com.apple.webarchive');

