1

I am developing a tool to add a HTML-ized version of a text to the clipboard. Currently, I'm using applescript to manipulate the clipboard.

Unfortunately, I realized that in addition to the entries I ask it to add, it also adds the entry styled Clipboard text. This, I believe, causes applications like Pages to fail to paste the HTML-ized version.

To see the current state of the clipboard I use (with the word "while" in the clipboard).

$ osascript -e "the clipboard as record"
«class utf8»:while, «class ut16»:while, string:while, Unicode text:while

I add the HTML variant as follows (the HTML is encoded as a hex string):

$ osascript -e "set the clipboard to ((the clipboard as record) & {«class HTML»:«data HTML3c6d65746120636861727365743d277574662d38273e3c7072653e3c7370616e207374796c653d22636f6c6f723a20236364303063643b223e7768696c653c2f7370616e3e3c2f7072653e0a»})"

After adding the HTML variant, the clipboard contains:

Unicode text:while,
string:while,
styled Clipboard text:«data styl01000000000010000E00030000000C00000000000000»,
«class utf8»:while,
«class HTML»:«data HTML3C6D65746120636861727365743D277574662D38273E3C7072653E3C7370616E207374796C653D22636F6C6F723A20236364303063643B223E7768696C653C2F7370616E3E3C2F7072653E0A»,
«class ut16»:while

Pasting this into Pages (-09) cause the plain text version of the text to be inserted.

However, when copying the same from the gmail editor in Chrome it can be pasted with highlight information into Pages. In this case the clipboard contains:

«class utf8»:while,
«class HTML»:«data HTML3C6D65746120636861727365743D277574662D38273E3C707265207374796C653D22636F6C6F723A207267622833342C2033342C203334293B20666F6E742D73697A653A20736D616C6C3B20666F6E742D7374796C653A206E6F726D616C3B20666F6E742D76617269616E743A206E6F726D616C3B20666F6E742D7765696768743A206E6F726D616C3B206C65747465722D73706163696E673A206E6F726D616C3B206C696E652D6865696768743A206E6F726D616C3B206F727068616E733A206175746F3B20746578742D616C69676E3A2073746172743B20746578742D696E64656E743A203070783B20746578742D7472616E73666F726D3A206E6F6E653B207769646F77733A20313B20776F72642D73706163696E673A203070783B202D7765626B69742D746578742D7374726F6B652D77696474683A203070783B206261636B67726F756E642D636F6C6F723A20726762283235352C203235352C20323535293B223E3C7370616E207374796C653D22636F6C6F723A20726762283230352C20302C20323035293B223E7768696C653C2F7370616E3E3C2F7072653E»,
«class ut16»:while,
string:while,
Unicode text:while

My conclusion is that the difference is the styled Clipboard text entry.

So, over to the real question. How can I set the HTML entry without setting the styled Clipboard text entry.

I have tried several approaches. For example, I have tried to set the clipboard to only contain a string and a HTML entry, but still the extra entry is included. I have also tried to set the styled Clipboard text entry to the empty string but that also set all the plain-text entries to the empty string as well.

I don't think the pbcopy command line tool is powerful enough to do this, as it set the clipboard to a new value.

While I'm currently using applescript to do this, I'm open to using any tool available, preferably a scriptable one (so that I don't have to ask the users to perform a compilation).

1

After some investigation I found that you can manipulate the Pasteboard from Python, without having it introduce the styled Clipboard text entry. I reimplemented the ruby/applescript module in Python, so now I can copy syntax highlighted text from Emacs and paste it either into dumb applications like the terminal window or applications that can handle formatted text, like Pages.

  • Would you be willing to share a snippet of the Python? GitHub or here or on SO? Lots of people get very stuck on multiple clipboards on macOS. – bmike Apr 27 '17 at 12:08
  • Yes, just click on the word "reimplemented" in the answer above. – Lindydancer Apr 28 '17 at 8:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .