The below macro works (mostly) in Safari, but not Chrome.
####Issue: When I copy rich text from a Chrome web page using a KM macro, the KM clipboard actions seem to treat it as plain text.
So, if I copy, for example, bold text from Chrome, and add plain text prefix and suffix on the Clipboard, the result is all plain text.
####How to I make this work in Chrome?
####After further testing I found:
Manual copy/paste from Chrome into TextEdit works as expected -- all rich text formatting retained.
Running the KM macro with Safari retains some, loses some, rich text formatting
Running the KM macro with Chrome loses all rich text formatting
Safari creates an RTF version for you when you copy,
whereas the Chrome clipboard only contains the copied HTML and some plain text translations
The trick will be, on the Chrome side, to unpack the hex-encoded HTML, pass it through textutil for an HTML â RTF rewrite, and place the results in the clipboard.
( I think some recent posts contain perl + textutil command lines for doing this kind of thing )
Well, it seems to me that if I can copy from Chrome and paste to TextEdit, and all formatting is retained, then I should be able to do the same with KM.
Maybe KM needs to read the Clipboard in a different way?
Thatâs the same as saying âI can copy this image, and my OCR app can convert it to text, so therefore Keyboard Maestro should be able to read the text tooâ. Not as obvious, but it is the same principle.
Keyboard Maestro can read either the plain text (in a whole bunch of encoding flavors), or the styled text (in a bunch of flavors). HTML is neither plain text nor styled text as far as Keyboard Maestro is concerned (because itâs not supported as styled text by NSAttributedString) - this is exactly why the clipboard has multiple flavors. Since Chrome actually knows how to render HTML, it should probably put styled text on the clipboard as well.
In this case, if Chrome copies the HTML, and no other styled text information, then youâll need some sort of processing to convert the HTML into styled text.
I'm not sure I buy the OCR analogy , but I get your point: KM will only use what is actually on the Clipboard, and not create anything like RTF from HTML.
That's actually very easy to do in AppleScript, as I have recently learned.
So is there a direct way from KM to determine if RTF data is on the Clipboard, or do I need to do that in AppleScript (or Javascript) as well?
You should be able to use the Clipboard condition and the "conforms to" check to see if there is RTF and/or HTML on the clipboard (although that wont tell you if there is something else that NSAttributedStrings can read as rich text).
It seems, for example to evaluate âpublic.htmlâ with a Chrome clipboard to true, but com.apple.webarchive with a Safari clipboard to false.
( but I may be missing something there â havenât yet found a symbol for which it will return true in the presence of Safariâs «class weba» clipboard content, which can be hex-unpacked to a WebArchive plist containing a key for HTML)