Delete superscripted text from clipboard

I want a macro to remove superscripts from text on the clipboard. For example, to convert this:

Xxx Xxx xxxx, 1Xxx aXx xxxx 2bxxx cxx Xxx 3dxxxxx, xxxxxxxxx xx Xxx exxxxxxxx; xxx xxx 4xxxx xxxx 5fxxxxxxxx

to this

Xxx Xxx xxxx, Xxx Xx xxxx xxx xx Xxx xxxxx, xxxxxxxxx xx Xxx xxxxxxxx; xxx xxx xxxx xxxx xxxxxxxx

My first thought was to use the Filter System Clipboard or For Each Item actions but it seems they do not work on styled text. Neither does Regex, it seems.

Any advice?

You haven't said where the text is coming from, nor what format it is in, not where it'll end up. So any answers will be pretty general.

Assuming there's RTF amongst the Clipboard flavours you could bounce the text to HTML and back to RTF, stripping the HTML superscript entities in-between. You'll probably lose some formatting, but that's HTML for you.

Demo:

Strip Superscripts.kmmacros (3.4 KB)

Image

You might be able to do similar with the raw RTF, but that's a whole new world of pain -- so see if the above is good enough first.

1 Like

@Nige_S I’m copying this text from a website, and not sure how to answer your other questions. I’m attaching a superscripts.docx to show what it’s like when pasted from the website into Word.

superscripts.docx.zip (12.2 KB)

When I tried your macro on text freshly copied text from the website I got this error message:

One way I can remove just the superscripts is via Microsoft Word’s advanced find and replace which allows searching for formatting. Should I write a macro to do that? I’m trying to use VBA less and KM more.

If so, I’m guessing the slowest step would be opening/closing Word. I’m used to being able to do that in the background with VBA. We all use the tools we know best, and to be honest even though I’ve spent years with KM, I’m still a novice/hack.

What web site?Is it publicly accessible so people can test against it?

Which browser are you using?

What are you trying to Paste into at the end?

The macro is working for me when copying from Safari, as a couple of "Display Clipboard" Actions show:

...but for some reason you aren't getting RTF when copying.

The problem is that when you Copy the "donor" app loads the Clipboard with the selected data in a variety of, hopefully useful, formats (flavours). When you Paste the "receiving" app picks the format that it can make the best use of, then does its own thing to the data. So what ends up in Word doesn't tell us what you originally Copied!

Yep, still working for me -- both for small snippets and for a Command-A and Copying the whole page.

But try changing the options for the first Action. Click on its cog-wheel and untick both "Failure Aborts Macro" and "Notify on Failure":

That way, even if there's no RTF on the Clipboard the macro will continue and textutil will try to make sense of whatever is available.

Which answers the unanswered question -- you're using Chrome.

Short answer -- try in Safari instead. Or Copy from Chrome, Paste into TextEdit to "translate" to RTF, Copy what you Pasted then run the macro.

Longer answer -- Chrome has its own ideas about what should go on the Clipboard, RTF isn't part of that, you might be able to find some way to extract public.html and work directly on that with the search and replace.

1 Like

@Nige_S Yes, it works perfect in Safari. I had no idea which browser used would be important. Okay, I can live with that.

Thanks!

On variation in how each app's Copy command populates the clipboard, see, for example:

Thanks for the link. I did read it, and I would say the main Takeaway that I got from this thread is that the clipboard is complicated when it comes to rich text.

Unfortunately, I am often quite interested in styled text, especially wanting to find and replace based upon it as can be done with advanced find and replace in Microsoft Word. I want to learn how to do that using KM.

There will be an option in Chrome (or any other Web browser) to view the page source, via a menu item or key shortcut. So it would be possible to make a little macro to search the page for items wrapped in the HTML <sup> tag (either directly or by copying the page contents to the clipboard).

I won’t go into more detail because whether or not this idea is of any use, and how exactly it would be implemented in a macro, would depend upon user requirements and preferences! However, I am sure you will see the basic idea.

By the way, @Tony, for general browser usage, if you wish to use one from the Chromium family and want to keep Google at a safer distance, try replacing Chrome with Brave Browser.

I actually found a way to almost do what OP wants but in Chrome -- problem was that the Copied public.html had extra "bits" in that RTF didn't so after stripping the superscripts:

Obviously that just another search-and-replace to fix -- if those extra characters are consistent.

For completeness, here's the AppleScript I used to get the public.html format off the Clipboard and turn the binary object into KM-usable text:

set thex to (the clipboard as «class HTML»)
set f to (path to "temp" from user domain as text) & "temp.txt"
-- Writes to a file named "temp.txt" in "Macintosh HD:Users:[userid]:Library:Caches:TemporaryItems:temp.txt"
set newFile to open for access file f with write permission
set eof of newFile to 0
write thex to newFile
close access newFile
set newFile to open for access file f
set theHTML to read newFile
return theHTML

I'm sure the ObjC gurus here could come up with something better!