I’ve seen this topic raised in a number of forum threads. Here’s a fast and robust technique that I’m currently using.
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 to be faster and more robust that others I’ve tried (e.g., checking for an active Copy menu item).
TESTED WITH
• Keyboard Maestro 11.0.3
• Sequoia 15.1 (24B5055e)/MacBookPro18,2
• Mojave 10.14.16/Macmini6,2
• High Sierra 10.13.6/iMac11,1445
VERSION HISTORY
( expand / collapse )
1.0 initial version
2.0
a) Renamed macro to Get Selected Text or Clipboard if No Text is Selected to Get Selected Text, Otherwise Clipboard.
I'm likely suffering from the PBKAC disease. I'm attempting to use the Macro as the start of something longer. There is something I'm not understanding about variables. (well there's alot I don't understand).
Get Selected Text's last action - "Not Required" is enabled it correctly displays my selected text. - however that display halts my macro run.
When that action is disable and I use the debugger and variable inspector, %local_Text% is empty.
Your Variable is empty because you’ve provided no Value to it.
If the Macro from Jim would provide a global Variable with the text you probably wouldn’t see an empty value. But that is not the case. It is a Local Variable.
A Local Variable stores its value only for the time of a single Macro Instance. They aren’t readable from other Macros (for example SubMacros) as long you don’t use them in parameters as TriggerValue for them.
If you want to use Variables between Macro Instances you’ll have to use Instance Variables because they don’t belong to single Instances. If a Macro has SubMacros every Instance Variable of this Instance could be shared between all SubMacros and the Main Macros running Instance. But if the instance is cancelled due to Macro ending the Values are all gone like if you’ve used Local Variables.
I hope this makes everything more clear to you how Variables work.
Hi, @mlevison; I should have more clearly described the original macro. It was not designed to be called, instead it was intended to demonstrate a technique, thus the actions could be embedded within your macro.
With that said, it could be revised to be used as a submacro or subroutine; the latter being my recommendation. If you’d like help with that, please let me know. I’m away from my computer now, but could help about five hours from now.
@_jims the subroutine hint was magic. It did the job. I've no idea how to create a subroutine, so I dupilcated an existing one and then copied your actions over. Adding a return value was easy enough.
@Nr.5-need_input As a recovering programmer the comment local variables made sense. I just needed Jim's hint to go create a subroutine.