Copying System Clipboard as Styled Text Converts It to Plain Text

I'm looking at a thread from four years ago:
Paste Without Formatting, But With Link

It seems to work for others but I am having trouble getting a styled clipboard to work in my particular macro. My context is that I am copying a list of links, sometimes more than 20, in a single โŒ˜C copy action. It seems as if, when I copy the current System Clipboard to the Temporary clipboard in order to get a blank line and a line with the date at the top, then style the Temporary clipboard, what gets pasted is the text version, the titles with the URL following in parentheses.

image

Here's the original System Clipboard, pasted into a new TextEdit document:

image

And here's the styled Temporary Clipboard, pasted into the target document, with the font and colors set:

At some point, within those two actions above, the formatted text got converted to plain text. I doubt that it's in the Apply Style action.

My supposition is that somehow, using `%SystemClipboard% in the definition of the Temporary Clipboard, the clipboard contents got converted to plain text before being saved as styled text. I have the "styled text" option selected in the "Set Clipboard" action.

Am I doing something wrong? How else can I prepend text to a styled clipboard and keep the styling, i.e. keep the URL hidden in the formatted text link?

The only thing I've come up with is to open a temporary TextEdit file, paste the clipboard, move to the top, insert the blank line and date, and copy the TextEdit contents.

That's an awful kludge, slow and opening extra app windows, in a macro that I would like to be relatively silent, just archiving the current tabs with a date while I quickly get back to what I was doing.

IIRC, the %SystemClipboard% text token only passes the plain text flavour of the clipboard's contents, which is why you're losing your RTF linky goodness. And, AFAIK, you can't directly prepend/append to a clipboard's contents.

Easiest workaround would be two "style then paste" operations, one for your boilerplate and a second for your links.

If I can nuance that a bit, and suggest an experiment:

Given the usual choice of multiple NSPasteboardItem types (or in the terms used by the Keyboard Maestro wiki, "clipboard flavors") %SystemClipboard% has to make a choice of which of the available flavors to use.

  1. %SystemClipboard%'s first choice seems to be to use any available public.utf8-plain-text NSPasteboard item that it finds.
  2. TextEdit creates public.rtf as well as public.utf8-plain-text and public.utf16-external-plain-text pasteboard items, but the public.rtf is not the first choice when that token is processed.

If however, we delete the plain text flavors, leaving public.rtf as the only flavor that %SystemClipboard% can use, it will not refuse to do so.

If we copy something like this from TextEdit:

then:

Purge plain NSPasteboard items before evaluating %SystemClipboard%.kmmacros (2.3 KB)

will lead to:

Yes, but can you then prepend that with plain text? As I understand it, what you've done works because you've got an RTF "blob", and you won't be able to add to that via simple token concatenation.

We can prepend it with a token, I think (whether we need to describe tokens as "plain text" may be another question)

But that's in a "Display Text", not going to a named clipboard where it can be restyled.

I think I can see where you're coming from, though. Much easier to do the combination in a "Display" that @August can copy'n'paste from than to bounce through TextEdit or similar.

[EDIT] Apparently @Nige_S and @ComplexPoint had I conversation while I was writing up the following note. I had only seen up to this comment of @Nige_S's. I've done some more experiments that point to the Apply Style action, but I'll read the rest of the above conversation before I comment further. Out of sequence comment follows:

Thanks @Nige_S,

I thought of that too, after I made the above post. And I haven;t figured out a way to make it work. There seems to be a bit of a Catch-22 that I haven't unravelled.

I can create a temporary or named clipboard for the sytled text that I want to prepend, but when I paste it in place, the act of pasting appears to overwrite what I have in the System Clipboard, so when I then paste that in, I just get a second copy of the previous paste.

If I attempt to copy the System Clipboard somewhere else, to a variable or to another clipboard, using the Copy Clipboard to Clipboard action:

image

... I still get the text version. I would suggest this is a bug. Copy Clipboard to Clipboard "should" preserve the entirety of the clipboard contents and not lose the "RTF linky goodness". @peternlewis, what do you think? Is this a bug? Maybe there should be a toggle option in the Action to choose text copy or full linky goodness copy.

It appears that the only way I can preserve the copied links, as links, is to keep them in the System Clipboard, do no other Paste actions while holding onto that clipboard, and make no modifications to the System Clipboard other than the Apply Style to Clipboard action.

So one way that I might be able to work around this would be to insert the prepend text first and then load the System Clipboard with my links. But that means switching apps, maybe even switching Desktops if the archive file is already open somewhere else, and I don't know how to restore the context of the original window to be able to grab the links.

IF the archive file is currently closed, I could open it, paste in the date info, close it again, which would hopefully restore the context of the original app, grab the links, then reopen the archive file and paste in the links after the prepend date that I just pasted.

Opening that archive file twice, before and after grabbing the links, is just about as awkward as opening a temporary TextEdit file in which to do the assembly of the link info block to paste into the archive file. I would prefer to not see extraneous windows popping up and closing again, just to be able to grab a complete copy of what I am looking at.

At the moment, I'm still stuck. The Catch-22 described above does not seem to have a simple workaround and yet it seems on the surface to be a simple task.

Don't forget that you can copy directly to your named clipboard, avoiding that problematic bounce.

But you should try:

  1. Copy your text and links to the System Clipboard
  2. Do your stuff with the boilerplate
  3. "Paste from Named Clipboard" the boilerplate -- which will set the System Clipboard to boilerplate
  4. "Delete Current System Clipboard" -- gets rid of the boilerplate, should "promote" your copied text and links to the current System Clipboard
  5. Paste your text and links

It'll help if we knew the app(s) you are copying from and pasting to, and ideally some sample text, so we can reproduce the problem. Apps have control over the formats they put onto, and take from, the System Clipboard so the closer we can get to what you're doing the better.

Thanks @ComplexPoint, your demo seems to verify that this should work, and also gets to the heart of the problem. Thanks for that macro. I definitely want to give that a try. Your second demo, with the date added, is very encouraging.

Forget? I don't think I ever knew. But I'm not sure that it will work. I'm not highlighting the entire set of links and then copying, I'm copying links out of Tabs Outliner which treats โŒ˜C a little differently. If I have a "Window" line selected, typing โŒ˜C will copy the entire entry for that windows with all of its tabs as links. I just have to click at the "Window" line, not figure out which lines are in and not. I don't think I would be able to copy directly to a named clipboard with that.

Your other suggestion,

I also had no idea that that was possible. Combined with @Nige_S's suggestion about deleting clipboard flavors, that may do the trick.

This may be one source of the problem and what requires the workarounds above, when the concept works in general for other people.

As I mentioned above, the source of the links is the Tabs Outliner Chrome Extension. The target is my own app, DeskSpaceNotes, which is a renaming of TextEdit using an older version of TE, v1.8, the latest that I could find source for so that I could rename it. Having a dedicated version of TE allows me to have separate list of Open Windows and Recent Files from the un-dedicated, up-to-date version of TE, v1.15, as shipped with Catalina, that I use for general composition when I'm not using Markdown or Open Office Write. The older TE may indeed handle different flavors of clipboard contents differently than newer ones, and @ComplexPoint's suggestion may help work around that.

I have also found that I can toggle the problem on and off, using only the System Clipboard and not attempting to insert anything else, when I enable and disable the Apple Style to System Clipboard action. Again, this appears to work for other people so the glitch may be due to one or the other of my oddball apps.

I'll be able to try this later tonight, I hope. Thanks to you both!

1 Like

Still works with a named clipboard, I think.

I am getting a re-styled named clipboard, after copying formatted material from TextEdit, for example, with this:

Remove Clipboard Flavors.kmmacros (3.7 KB)

Where can I find a list of the "flavors" to choose among?

If public.rtf is the best clipboard flavor to use in preserving links, why remove the two text flavors instead of removing everything except public.rtf?

I tried removing those two text flavors, and I still got plain text in the clipboard.

Never mind. I found %SystemClipboardFlavors%. When I display that, for the clipboard content that I'm capturing from Tabs Outliner, I get:

com.apple.traditional-mac-plain-text
org.chromium.source-url
org.chromium.web-custom-data
public.html
public.utf16-plain-text
public.utf8-plain-text

So I don't want to force public.rtf because it isn't there.

I'll try removing all three of the *.plain-txt flavors and see what happens.

For pasteboards which contain no public.rtf,
the best approach is to keep public.html, and delete all others.

macOS is good at:

  1. producing formatted text from public.html, and
  2. general public.html โ‡„ public.rtf conversions.

I found %SystemClipboardFlavors% .

See also:

token:NamedClipboardFlavors [Keyboard Maestro Wiki]

Here is a general approach to purging all flavors other than public.rtf and public.html from the system clipboard:

Keep Rich text System Clipboard flavors only.kmmacros (5.3 KB)

1 Like

image

Oooh -- that's sweet!

Otherwise the macro seems to leave me with a "Non-Text Clipboard" when run on even a single unstyled word copied from this thread -- flavor is still reported as public.html, but adding a "Display Text %SystemClipoard%" action to the end of the macro gives me

I've a feeling that's a problem at on my side, though.

Yes, any Paste must be done from the system clipboard, of which there is only one. So any time you paste from any source, that source must be copied in to the system clipboard and then Command-V is used to cause the Paste to happen.

Copy Clipboard to Clipboard copies all flavours (unless explicitly marked as transient).

So either there is some confusion on your part of what is happening, or the app is not actually putting the flavours on the clipboard, or the app is marking the flavours as transient or otherwise using private flavours.

I, too get that same "No Text in Clipboard" message, when I strip all the "plain-text" flavors out of the clipboard. And I, too, think it's likely that I have a problem on my end.

I have more things to try...

Thanks for the confirmation, Peter. That's what I figured. I sill haven't tried Nige's suggestion:

which again, in theory, ought to work.

Peter(@peternlewis), do you have any idea why a straight paste of URL links in the clipboard works, pasting in formatted text with hidden hyperlinks, but doing Apply Style to Clipboard and then pasting gets the text version instead?

That seems to be the core of the matter, it's mostly KBM actions, and none of the fiddling with the flavors seems to work.

The specific purpose of my original problem has been to paste a list of captured hyperlinks into the top of a file, immediately following the title line, without affecting the formatting of the title line, while also restyling the pasted text, because the default font and size is unacceptable.

My original attempt, and all of the previous discussion, has been focused on what it takes to use the Apply Style to Clipboard action. And that has not been working.

Rather then the debug the elegant solution, I have discovered that for this specific purpose, I can:

  • navigate to the end of the title line,
  • add a new line,
  • paste in the raw, captured list of hyperlinks,
  • count how many lines are in the clipboard,
  • select upwards from the end of the paste using hey series of โ‡งโŒ˜โ†‘ keystrokes,
  • overshoot the top of the file by a few lines,
  • come back down with one โ‡งโŒ˜โ†“ keystroke to unselect the title line,
  • and finally apply font and size styling to be selected, newly pasted hyperlinks.

It solves my problem for this specific case, but does not provide an elegant, widely applicable solution. Still, I have a way forward to go on my specific workflow issue, which allows me to get this particular troublesome issue off of the front burner, while allowing me to continue experimenting with a more elegant solution at my leisure.

The specific purpose of my original problem has been to paste a list of captured hyperlinks into the top of a file, immediately following the title line, without affecting the formatting of the title line, while also restyling the pasted text, because the default font and size is unacceptable.

My original attempt, and all of the previous discussion, has been focused on what it takes to use the Apply Style to Clipboard action. And that has not been working. It persisently does apply the style while also reformatting the hyperlinks as text. @ComplexPoint has gotten it to work, but I haven't, and I haven't yet "tried everything".

Rather then the debug the elegant solution, I have discovered that for this specific purpose, I can do a case-specific workaround with this sequence:

  1. navigate to the end of the title line,
  2. add a new line,
  3. paste in the raw, captured list of hyperlinks,
  4. move down a line to capture everything that is the title format
  5. count how many lines are in the clipboard,
  6. select upwards from the end of the paste using hey series of โ‡งโŒ˜โ†‘ keystrokes,
  7. overshoot the top of the file by a few lines,
  8. come back down with one โ‡งโŒ˜โ†“ keystroke to unselect the title line,
  9. and finally apply font and size styling to the selected, newly pasted hyperlinks.

It solves my problem for this specific case, but does not provide an elegant, widely applicable solution. Still, I have a way forward to go on my specific workflow issue, which allows me to get this particular troublesome issue off of the front burner, while allowing me to continue experimenting with a more elegant solution at my leisure.

Sure - the initial copy has some random set of flavors that include the stuff you want, where as the Apply Style to Clipboard action reads the styled text, applies the styles, and then writes just the styled text back to the clipboard, resulting in styled text.

So the former has some different flavour that it being used.

Styled Text on the Mac is a very complicated subject, and it is just one possible complicated thing of the set of flavours in the clipboard.

Is there some way of discovering which flavors have "the stuff I want", in particular, hyperlinks as links rather than simply a text name and a URL, and somehow forcing the clipboard to contain only that? Does the Apply Style to Clipboard action only grab a "styled text" flavor, if there is one?

I've been presuming that a flavor named with "text" would have a hyperlink's text and URL separated and would paste them in separately. But if some "styled text" flavors support hyperlinks and some don't, how can I tell?

Is there a standards group for clipboard flavors? Is there a master list at developer.apple.com?

Is there some way of examining the actual contents of the clipboard, all the different flavors and what info they contain?

There are hyperlinks in there, they show up as links, not separated text and URL, when I paste the clipboard without any Apply Style, and they have a font and size, different from the default where they are being pasted. All I want to do is change that font and size, without losing the hyperlink formatting.

@ComplexPoint seems to be able to do it while @Nige_S and I seem to be unable to duplicate that, so obviously there are weird idiosyncrasies that are not helped by my using oddball apps. How can I tease out the differences?