Chrome Issue: Getting Rich Text on the Clipboard?

Running KM 6.4.8 on Mavericks (10.9.5).

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:

  1. Manual copy/paste from Chrome into TextEdit works as expected -- all rich text formatting retained.
  2. Running the KM macro with Safari retains some, loses some, rich text formatting
  3. Running the KM macro with Chrome loses all rich text formatting

See my example below.

####My Macro:
BRW- TEST Combine Rich & Plain Text on Clipboard.kmmacros (5.0 KB)

URL Tested:

####Copy the below selected text from Chrome or Safari:

####Manual Copy/Paste from Chrome into TextEdit:

####Chrome Result:

####Safari Result:

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 )

1 Like

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.

Hi Peter. Thanks for clarifying. :smile:

I’m not sure I buy the OCR analogy :astonished: , 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).

Am I right in thinking that the clipboard conforms to test recognises symbols in column 2 but not col. 1 of ?

It seems, for example to evaluate ‘public.html’ with a Chrome clipboard to true, but 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)