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
-
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.
-
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.
-
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.
-
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-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