I’ve seen this topic raised in a number of forum threads. Here’s the techique that I’m currently using.
Please comment if you have developed other techiques that you have found useful.
PURPOSE
The purpose of this macro is to demonstrate a method to populate a Keyboard Maestro variable (local_Text) with the currently selected text, or if no text is selected, the contents of the System Clipboard. The text can be selected in a browser or any macOS app that supports text. Note that this technique uses the System Clipboard, but it does not modify it.
I’ve found this method more robust that others I’ve tried (e.g., checking for an active Copy menu item).
EXAMPLE MACRO
Here’s one example available on the Keyboard Maestro forum: Augment or Filter Text
TESTED WITH
• Monterey, v12.1; Mojave 10.14.16
• Keyboard Maestro, v10.0.2
• MacBookPro16,1, Macmini6,2
VERSION HISTORY
1.0 initial version
DOWNLOAD Macro File:
Get Selected Text or Clipboard if No Text is Selected.kmmacros (27 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.