After Copying Text From a PDF, Remove Carriage Returns and Paste

I often copy text from PDFs of books and articles, as well as from images, and paste the text into Scrivener or Word. I often use the app TextSniper to grab text from images and PDFs. Then when I go to paste the text into Scrivener or Word, the text often has a carriage return after every line, like this (I inserted the ¶ symbol after each line below for clarity):

The latest draft, which is broadly similar to one released¶
on Friday, called on nations to return next year, instead¶
of 2025, with stronger pledges to cut planet-warming¶
emissions in this decade. It urged wealthy nations to “at¶
least double” by 2025 the financial aid that they provide to¶
developing countries to help them adapt to heat waves,¶
floods, droughts and wildfires.¶

To get rid of the carriage returns after each line, I have to scroll up from the end of the pasted text, highlighting all of the pasted text, then press my keyboard shortcut (F4) to trigger TextSoap to remove the extra returns.

It's a pain to have to scroll up from the end of the pasted text to highlight the text. I want to create a macro that will highight the text I just pasted (by selecting the text from the end of the just pasted text to the beginning, where my cursor originally was when I pressed CMD-OPT-V), then the macro will trigger the TextSoap shortcut (F4) to remove the extra returns.

I'm new to KM and am not a programmer. Any ideas? Thanks!

1 Like

Hey David,

There's no great way to do that on demand in any generic application. Many people want it to be so, but it's not – because neither the macOS or most apps have the capability to know what the selection is in a text editing context.

Usually the best you can do is preprocess your text before pasting it.

BBEdit has a paste-and-select command that selects what you just pasted. You could ask the Scrivener developer for such a feature.

On the other hand – since Scrivener's find capability is not bad, and Keyboard Maestro can set the find pasteboard there's a sneaky way to do this.

It won't work in Word, although there's a possibility that you could use AppleScript to do something similar.

-Chris


Scrivener ⇢ Paste and Select v1.00.kmmacros (8.3 KB)
Keyboard Maestro Export

Based on your description, it doesn't sound like you need to copy the text again after it's been pasted, or even to run it through TextSoap. When OCRing text with TextSniper, you can just turn off the option to keep line breaks:

CleanShot 2021-11-13 at 14.15.46@2x

And for text copied from other sources, you can use KM to remove the link breaks from the clipboard:

You could even have KM do this automatically whenever you switch to Word or Scrivener or any other application you like:

Auto Remove Line Breaks from Clipboard.kmmacros (1.7 KB)

1 Like

Thank you both... These are super helpful!

1 Like

I tried the macro which filters the clipboard like Gabe suggested--the macro works in Word, but not in Scrivener. All I did was add Scrivener as an additional triggering application. When I paste the text into Scrivener, it still has the returns after every line:

The latest draft, which is broadly similar to one released¶
on Friday, called on nations to return next year, instead¶
of 2025, with stronger pledges to cut planet-warming¶
emissions in this decade. It urged wealthy nations to “at¶
least double” by 2025 the financial aid that they provide to¶
developing countries to help them adapt to heat waves,¶
floods, droughts and wildfires.¶

Not sure why Scrivener is working differently than Word.

In my opinion this kind of automation is usually a bad idea – it works when you don't want it to and doesn't when you do want it to.

Much better to take direct control.

You may have to refresh the apps selected in the macro group for it to work – it works here with both Word and Scrivener without a problem.

-Chris

Auto Remove Line Breaks from Clipboard.kmmacros (2.5 KB)
Keyboard Maestro Export

I think I figured this out. The carriage returns are stripped when pasting some paragraphs from the PDF, but not when pasting others from the same PDF. I used ABBYY FineReader to OCR the PDF. The odd behavior is probably from using FineReader. If anyone has a suggestion on how to fix this, I'd love to hear.

Hey David,

Paste the problematic text into a Keyboard Maestro Comment action, and post that to the forum – so we have something to test.

-Chris

I figured it out. I had scanned a book, and on some of the 2-page spreads, the inside margins of the 2 pages was really close together because the book binding was messed up, which I think confused ABBYY Fine Reader:

I used this command to split those pages apart, and now it works fine when copying and pasting:

image