Macro: Paste Text Unformatted from Copied Text or macOS Screenshot

PURPOSE:
Paste as Plain Unformated Text using same hot key, whether the source in the Clipboard is actual text or a Screenshot image of text.

MACRO:
Paste Text.kmmacros (18.9 KB)

Click to Show Image of Macro

I'm sure many of us have a method to paste copied text from one document to another so that the pasted text matches the formatting of the destination document. For years I have used a Keyboard Maestro Macro with the hot key βŒƒV to paste text without formatting.

But what if the text I want to paste is from an image rather than actual text? Quite often I will use the Mac's native screenshot function to capture an area which has some text in it.

So, here is a Macro that pastes the Clipboard as unformatted text, whether that text was captured as actual text or as an image of text.

VIDEO:
In the video example I first copy some formatted text to the Clipboard and use βŒƒV to paste to another document, without formatting. I then capture an area screenshot of an image of some text to the Clipboard and use the same hotkey βŒƒV to paste this as unformatted text.

Paste Text

EDIT:
The above is the original very simple version of the Macro. @_jims has made a Macro that copes with more varieties of image input, trims newlines when needed and much else. Click this highlighted text to go to it.

10 Likes

Hi, @Zabobon. Thanks for sharing your macro.

Interestingly on my system, the condition has an image does not seem to evaluate as expected.

When I copy an image into the clipboard and run the following macro, I see in large text does not have an image and the following window.

Evaluate Clipboard.kmmacros (17 KB)


I'm using:

β€’ Keyboard Maestro 11.0.3
β€’ Sequoia 15.1 (24B83)/MacBookPro18,2

What am I missing?

How are you copying the image? I see a file path on there (though not the other stuff you'd expect from eg selecting an image file in Finder and copying).

2 Likes

Yes. That's why I did not use the condition "has an image" but instead used "does not have text".

What I found worked was "does not have text" to trigger the OCR and "does not have image" to leave as text.

1 Like

As @Nige_S asks - how are you copying the image. What does Keyboard Maestro display in the Clipboard History.

It looks like what you have actually copied is an image reference, not an image. You likely could use the Set Clipboard to Image filter in the clipboard history to convert it to an image.

That said, if the clipboard has an image reference, then the has image condition should return true, so I will adjust that to allow for image references as well.

2 Likes

Oops, @Zabobon, my mistake. I originally had the condition that is in your macro (i.e., does not have text). When that didn't work as expected (see more below), I changed the condition to has an images. Sorry for the confusion with regards to the condition.

@Nige_S, I think you are onto something.

I use a really nice macOS application named CleanShot X. When a screenshot is taken, a small panel appears. The panel includes a Copy button. After using that button, pasting normally works as expected. For example, if I'm in the Discourse editor for this forum, paste will upload the copied image.

But for some reason, the copied image does not cause the does not have text condition to be true.

After seeing @Zabobon's and your reply, I did some more testing...

If I use the macOS native screenshot (to the clipboard) keyboard shortcut (β‡§βŒ˜4), the does not have text condition is true. Under these circumstances, clipboard info is...


In conclusion, @Zabobon, if you want to support CleanShot X (and possibly other methods of populating the clipboard with an image), maybe the Switch/Case should key off clipboard info.

When the clipboard includes plain text, clipboard info returns...

When it includes rich text, clipboard info returns...

@peternlewis, thanks for weighing in.

After using the CleanShot X Copy button (as described above), here's what (as an example) Activate Clipboard History Switcher displays...

If my understanding is correct, it seems that that change would handle the situation I described above.

Hi @peternlewis - yes, like @_jims I found the clipboard condition has an image not to be reliable, which is why I used does not have text which in use I found to work consistently. In my case I only wanted to toggle between two possibilities (I have copied some text or I have copied an image) but I would rather have used has an image as that was what I was really testing for.

As an aside and talking about OCR and Apple Intelligence - I have been so impressed recently by how accurate the Mac is at recognizing text in images. It is almost scary how it can find text when I search through my huge Photos Library in the Photos App. It will find and match text that is on wine bottle labels in photographs I took years ago. And I was even able to OCR a handwritten note in Korean, which the Mac translated for me. And now in macOS Sequoia 15.1 we have the first bits of Apple Intelligence to play with. Exciting times.

2 Likes

Hi, again, @Zabobon.

Please note that does not have text also doesn't work under the conditions I described above.

1 Like

It sounds like you already have a solution that works for you? For your use of CleanShotX you can use a condition that tests what is in Clipboard Info?

I'd be very happy if you wanted to upload a Macro to this thread that works for both Native macOS Screenshots and CleanShotX Screenshots.

The concept of the Macro is just to use OCR if the clipboard has an image in it and paste plain if it doesn't. As I said it's an extremely simple concept but I thought it worth sharing as I've been finding it very useful.

1 Like

Well, @Zabobon, at the time I hadn't, but I accepted the challenge.

Unless I muffed something, this seems to also work for image files in the Finder that are copied.

Download: Extract Text from the System Clipboard and Paste Plain Text.kmmacros (61 KB)

Macro-Documentation

PURPOSE

This macro will process the contents of the %SystemClipboard% and pastes plain text.

  • If plain text is copied, the same text will be returned.

  • If rich text is copied, the text will be returned.

  • If a screenshot of a selected area is captured to the clipboard (using the macOS native method), any text in the screenshot will be returned. Apple Text Recognition is used, here and below, for the OCR.

  • If a screenshot is generated with CleanShot X and the screenshot is copied using the Copy button, any text in the screenshot will be returned.

  • If one or more Finder files are selected, the names of the file(s) will be returned. (See exception below.)

  • If the secondary hot key, by default, the hot key that includes the Option (βŒ₯) key, is used to trigger the macro...

    • If an image file is copied in the Finder, any text in the image will be returned. File types tested: HEIC, JPEG, JPEG-2000, OpenEXR, PNG, TIFF. Interestingly, Apple Text Recognition does not seem to be able to extract text from PDF documents.
  • When OCR is used, the ending newline is removed before the text is returned. This behavior can be changed with a setting.

  • When the OCR text includes multiple lines, a dialog will appear that provides an option to combine the lines. This behavior can also be changed with two settings.

SETTINGS

  1. local_ModifierForSecondaryTrigger : βŒƒ|βŒ₯|⇧|⌘ (default: βŒ₯)

    A modifier, one that is not in the primary hot key trigger, that is in the secondary trigger.

    When the macro is triggered using the the secondary, if an image file is copied in the Finder, any text in the image will be returned. Conversely, if one or more Finder files are selected and if the secondary hot key is not used, the names of the file(s) will be returned.

  2. local_RemoveOcrEndingNewline : 0|1 (default: 1)

    When the macro uses OCR (Apple Text Recognition), a trailing newline, i.e., \n, is included in the text.

    If the setting local_PromptToCombineOcrLines is 0, then a value of 1 for this setting will cause the newline to be removed.

    If local_PromptToCombineOcrLines is 1, then this setting is used to set the initial value for the Remove Ending Newline checkbox in the dialog.

  3. local_PromptToCombineOcrLines : 0|1 (default: 1)

    When the macro uses OCR, newlines in the text are retained. For example, if an image has two lines of text characters, the OCR will return two lines of text.

    A value of 1 for this setting will cause the macro to display a dialog if the returned text includes more than one line. The dialog will provide a Would you like to combine the lines? prompt and a Remove Ending Newline checkbox.

  4. local_IfNoPromptToCombineOcrLinesThenCombine : 0|1 (default: 1)

    If local_PromptToCombineOcrLines is 0, this setting will specify if the macro will combine multiple lines of text.

TESTED WITH

β€’ Keyboard Maestro 11.0.3
β€’ Sequoia 15.1 (24B83)/MacBookPro18,2
β€’ Mojave 10.14.16/Macmini6,2
β€’ High Sierra 10.13.6/iMac11,1445

VERSION HISTORY

1.0 - initial version

2.0 - Added the four SETTINGS and the related features.

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 15.1 (24B83)
  • Keyboard Maestro v11.0.3

1 Like

In my case I realised quite quickly that this wouldn't work for my workflow as I often copy files in the Finder and then want to paste their names as plain text. So if I select and copy a file called Image.jpg in the Finder and then paste it as plain text using βŒƒV, I want to get "Image.jpg" rather than trying to look at its contents. I do this often when I want to paste a list of copied Finder files as text into an email.

But everyone's workflow is different (as you discovered when you tried to use my original Macro) so, I have edited my original post to explain the limitation you found and link to your version. I've also edited the title of this thread to specifically mention screenshots rather than "copied image" which I can see now was a bit ambiguous. :grinning:

1 Like

@Zabobon, I've updated my post above with Version 2.0 of Extract Text from the System Clipboard and Paste Plain Text. It accommodates your Finder requirement and includes some additional features that are partially controlled by four new settings:

  1. local_ModifierForSecondaryTrigger : βŒƒ|βŒ₯|⇧|⌘ (default: βŒ₯)

    A modifier, one that is not in the primary hot key trigger, that is in the secondary trigger.

    When the macro is triggered using the the secondary, if an image file is copied in the Finder, any text in the image will be returned. Conversely, if one or more Finder files are selected and if the secondary hot key is not used, the names of the file(s) will be returned.

  2. local_RemoveOcrEndingNewline : 0|1 (default: 1)

    When the macro uses OCR (Apple Text Recognition), a trailing newline, i.e., \n, is included in the text.

    If the setting local_PromptToCombineOcrLines is 0, then a value of 1 for this setting will cause the newline to be removed.

    If local_PromptToCombineOcrLines is 1, then this setting is used to set the initial value for the Remove Ending Newline checkbox in the dialog.

  3. local_PromptToCombineOcrLines : 0|1 (default: 1)

    When the macro uses OCR, newlines in the text are retained. For example, if an image has two lines of text characters, the OCR will return two lines of text.

    A value of 1 for this setting will cause the macro to display a dialog if the returned text includes more than one line. The dialog will provide a Would you like to combine the lines? prompt and a Remove Ending Newline checkbox.

  4. local_IfNoPromptToCombineOcrLinesThenCombine : 0|1 (default: 1)

    If local_PromptToCombineOcrLines is 0, this setting will specify if the macro will combine multiple lines of text.

Hi @_jims - this looks great! I will download it now. Good thing I uploaded my macro as now it looks like you've given me a better version of it (I had noticed there could be a problem with newlines and looks like you've addressed that too). Thanks a lot!

1 Like

Hi @Zabobon, just want to say thanks for creating this macro. For the last few days, I think I've used this macro close to 20 times a day. It's really handy. :slight_smile:

2 Likes

@_jims Sorry, I'm a new Keyboard Maestro user and on the first day I'm too stupid to understand parts of the macro. :slightly_smiling_face:

Paste text works fine. The problem arises when I want to extract text in an image and paste it somewhere. I thought the key combination for capture a MacOS screenshot area (β€œShift-Command-4”) would be enough to copy this area to the clipboard. But that does not work. Nothing happens at all. What works is: If I select an area of an already created screenshot with the mouse (dragging a area) and then copy it (Command-C), the text is correctly recognized and pasted.

Have I fundamentally misunderstood something here? I thought I was taking a screenshot of a specific area and then using the hotkey to paste the OCR-recognized content somewhere.

Unless you've changed the defaults, β‡§βŒ˜4 writes the screenshot to a file on your Desktop. You need to include the Control key to use the Clipboard -- βŒƒβ‡§βŒ˜4

3 Likes

Hi, @electricarts. It’s never a problem asking questions on the forum. That’s how most of us have learned.

You can do as @Nige_S instructed or use the macOS shortcut key you selected, move to the desktop, select the file, then trigger the macro with the secondary hot key. (If you use the primary hot key, the macro will capture the file name rather than extract the text from the image.)

3 Likes

@Nige_S Yes, of course i've used the wrong key command. I need the file in the clipboard and not at the desktop. :blush: Thanks!

2 Likes

@_jims Thanks! If I use the correct key command, it also works. :blush:

2 Likes