MACRO: Insert Action to Check Variable(s), v6.1

PURPOSE

Used during macro editing and simple ad hoc debugging, when triggered, this macro will add an action to display all macro variables in the selected actions. For example, the following Keyboard Maestro Actions (and many others) often include one or more variables in the parameters:

  • Set Variable to Text
  • Set Variable to Calculation
  • Split Path
  • Filter
  • Search using Regular Expression
  • If (actions within are also queried)
  • Switch/Case (actions within are also queried)
  • Execute AppleScript
  • Execute Shell Script
  • Use Variable
  • Pause Until
  • Looping Action: Until, While, Repeat, For Each (actions within are also queried)
  • Execute a Subroutine
  • Group (actions within are queried)

The macro has eight different triggers that are all combinations of three options:

  1. ⌘: Display format for Short Values* or Longer Values.**
  2. βŒ₯: Display Variables and pause or display and continue.
  3. ⇧: Add the action Below or Above the selected actions.
  local_ShortValueB [%Variable%local_ShortValueB%]

  local_LongerValueA
  β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ
  %Variable%local_LongerValueA%

  local_LongerValueB
  β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ β”ˆ
  %Variable%local_LongerValueB%

If display and pause is selected, a dialog will be displayed with three buttons:

  1. Continue : the variable window will remain open and the macro will continue; can be specified by pressing the return key.

  2. Close Window and Continue : the variable window will be closed and the macro will continue; can be specified by pressing the c key.

  3. Cancel : the macro exits; can be specified using the ESC key.

The primary trigger for this macro is βŒƒF2. It, or any of the other seven, can be tapped once, twice, or thrice. If tapped once, a Display Text action will be inserted below the selected actions using the format: local_ShortValue [%Variable%local_ShortValue%]

Mode 2: If βŒƒF2 is tapped twice, the previously used trigger (one of the eight) will be effectively repeated. This provides a quick method to insert several similar actions.

Mode 3: If βŒƒF2 is tapped thrice, a Prompt With List menu will be displayed with the eight options selectable:

     βŒƒF2    : Insert Below; Display Variable(s) and Pause; Short Value(s)
    βŒƒβŒ˜F2    : Insert Below; Display Variable(s) and Pause; Longer Value(s)
    βŒƒβŒ₯F2    : Insert Below; Display Variable(s) and Continue; Short Value(s)
   βŒƒβŒ₯⌘F2    : Insert Below; Display Variable(s) and Continue; Longer Value(s)

    βŒƒβ‡§F2  	: Insert Above; Display Variable(s) and Pause; Short Value(s)
   βŒƒβ‡§βŒ˜F2  	: Insert Above; Display Variable(s) and Pause; Longer Value(s)
   βŒƒβŒ₯⇧F2  	: Insert Above; Display Variable(s) and Continue; Short Value(s)
  βŒƒβŒ₯β‡§βŒ˜F2  	: Insert Above; Display Variable(s) and Continue; Longer Value(s)

When the PWL menu is rendered, each option will be labelled with the corresponding trigger. This provides a quick reference to the eight options.

KNOWN LIMITATION

Variables are presented in the order they are found within the XML of the actions. Many times this order differs from the order that the variables appear within the actions (in the KM Editor).

CALLING THIS MACRO

If this macro is called from another macro and a Parameter of 2 or 3 is specified, this macro will run in Mode 2 (use Previous Option) or Mode 3 (display PWL Menu of Options), respectively. This provides a method to run the macro in the alternative modes without using the two- and three-tap methods described above.

RELATED INFORMATION & ACKNOWLEDGEMENTS

In a Keyboard Maestro forum thread started by @Sleepy, Various Methods for Displaying KM Variables and Data, @noisneil first suggested a clever technique to insert a Display Text (in window) action; @ccstone subsequently contributed his take on the idea. Stealing shamelessly, I contributed another option: Insert⇨Actions to Display a Macro Variable. Since then, @noisneil created and shared Create Pre-Filled KM Actions.

Insert Action to Check Variable(s) supersedes Insert⇨Actions to Display a Macro Variable. It is faster, includes several more options, and does not require any set-up.
β€’ Thanks to @ccstone for assistance with the AppleScript syntax required to insert a new macro action before or after selected actions.
β€’ The multi-tap detection is done using the amazing HotKey Multi-Press Template, by @DanThomas.
β€’ Duplicates are removed using a JXA script (Remove Dup Items in List [Example]), by @JMichaelTX (RIP).

Insert Action to Check Variable(s) leverages the powerful scripting of the Keyboard Maestro editor features that @peternlewis introduced in KM v8.0. Every time I dig deeper into Keyboard Maestro, I’m amazed by the quality and depth of the application.

Insert Action to Check Variable(s), and the others mentioned above, are handy for simple ad hoc debugging. For true debugging see: Checkpoint System: An Additional Way to Debug in KM, by @tiffle.

TESTED WITH

β€’ Keyboard Maestro 10.2
β€’ Ventura 13.3.1 (22E261)/MacBookPro16,1
β€’ Mojave 10.14.16/Macmini6,2
β€’ High Sierra 10.13.6/iMac11,1445

VERSION HISTORY

Click to expand/collapse

1.0 - Initial version

1.1
a) In this comment, added the modifier key that toggles each of the three options. Also changed the order of the options.
b) Improved the name in the third Group action
c) Changed the color of the first Group action
d) Fixed three typos:
β€’ In the name of the first Group action
β€’ In this comment
β€’ In the name of the Comment actions included in the Group actions created by this macro

2.0
a) All variables in a selected action are now included in the added action; thus the name of macro was changed to: Insert Action to Check Variable(s)
b) If this macro is called from another macro and a Parameter of 2 or 3 is specified, this macro will run in Mode 2 (use Previous Option) or Mode 3 (display PWL Menu of Options), respectively. This provides a method to run the macro in the alternative modes without using the two- and three-tap methods.

3.0
a) Extracts variables that are in action Text fields if they are in the form:%Variable%some_variable%. (Before someone asks: the variables in calculations are not included. And based on this statement by @peternlewis that feature is very unlikely to be added to this macro.)
b) Extracts variables from the Split Path action. In that action, XML variable fields do not include a Variable key. I suspect that other such actions will be found as use of this macro continues.
c) Previously if a variable appeared more than once (e.g., in a Group action), the variable would be repeated. Duplicates are now removed thanks to a JXA script (Remove Dup Items in List [Example]), by @JMichaelTX (RIP).
d) Added a Display Progress action since the macro might run several seconds if a long action is selected (e.g., Group). The progress Title is: Searching for variables in the selected action

**3.1
**a) Extracts target variables in the Filter action.
b) Extracts capture group variables in the Search using Regular Expression action.

3.2
a) Extracts target variables in the Execute a Subroutine action.
b) Simplified error dialogs by using OK/ and :heavy_check_mark:︎Cancel Macro.
c) Fixed typos in this header comment.

4.0
a) Multiple actions (contiguous or otherwise) can now be selected and queried.
b) After the new action is created, it is selected in the editor.

4.1
a) In the AppleScripts that add the new action, disabled Failure Aborts Macro.
b) In several actions (If Then Else, Switch/Case, While, Until, Pause Until) extracts the object when it is a calculation (e.g., local_B when local_A = local_B).

4.2
a) When macro completes, the initially selected actions are reselected (thanks to some guidance from @ccstone).
b) Updated the TESTED WITH information.

4.3
a) Bug fix: Previous versions would capture text values that were not variables (i.e., not \%Variable\%.+\%).
b) Updated the TESTED WITH information.

5.0
a) Bug fix: Since v4.1, captured all calculated objects as a single variable. For example, a simple calculated object of 100 would incorrectly be assumed to be a variable.
b) Captures multiple variables within a calculated object, e.g., (local_ABC + instance_XYZ) / local_JKL.
c) For the triggers, changed the modifiers: 1) ⌘: Display format for Short Values or Longer Values, 2) βŒ₯: Display Variables and continue or display and pause, 3) ⇧: Add the action Below or Above the selected actions. Previously, the order was βŒ₯, ⇧, ⌘
d) Changed the display and pause to use a Display Text action and a Pause for User Input action.
e) Added the KNOWN LIMITATIONS section in this header comment.
f) Updated the TESTED WITH information.

6.0

a) Variables within calculated objects are now captured
b) Sets the size and position of the Display Text windows. As new windows appear, they are slightly offset on the x-axis.
c) Window include a title which includes the Window Number.
d) Plays the medium-volume Bottle sound after the action is inserted.
e) Optimized the search of XML for the variables.

6.1

a) Changed the default to Display Variable(s) and Pause.
b) Corrected a typographical errors in this header Comment and the Prompt With List.


Download: Insert Action to Check Variable(s).kmmacros (251 KB)

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 13.3.1 (22E261)
  • Keyboard Maestro v10.2


OPTIONAL MAIN MENU

2 Likes

I've updated the OP with Version 1.1 of Insert Action to Check Variable.

  • In the header comment, added the modifier key that toggles each of the three options. Also changed the order of the options.
  • Improved the name in the third Group action
  • Changed the color of the first Group action
  • Fixed three typos:
    • In the name of the first Group action
    • In the header comment
    • In the name of the Comment actions included in the Group actions created by this macro. @tiffle, thanks for bringing this typo to my attention.
2 Likes

I've updated the OP with Version 2.0 of Insert Action to Check Variable(s).

  • All variables in a selected action are now included in the added action; thus the name of macro was changed to: Insert Action to Check Variable(s)

  • If this macro is called from another macro and a Parameter of 2 or 3 is specified, this macro will run in Mode 2 (use Previous Option) or Mode 3 (display Prompt-with-List Menu of Options), respectively. This provides a method to run the macro in the alternative modes without using the two- and three-tap methods.

2 Likes

I've updated the OP with Version 3.0 of Insert Action to Check Variable(s).

  • Extracts variables that are in action Text fields if they are in the form:%Variable%some_variable%. (Before someone asks: the variables in calculations are not included. And based on this statement by @peternlewis that feature is very unlikely to be added to this macro.)

  • Extracts variables from the Split Path action. In that action, XML variable fields do not include a Variable key. I suspect that other such actions will be found as use of this macro continues.

  • Previously if a variable appeared more than once (e.g., in a Group action), the variable would be repeated. Duplicates are now removed thanks to a JXA script (Remove Dup Items in List [Example]), by @JMichaelTX (RIP).

  • Added a Display Progress action since the macro might run several seconds if a long action is selected (e.g., Group). The progress Title is: Searching for variables in the selected action

3 Likes

I've updated the OP with Version 3.1 of Insert Action to Check Variable(s).


2023-01-27 00.56.18

1 Like

I've updated the OP with Version 3.2 of Insert Action to Check Variable(s).

  • Extracts target variables in the Execute a Subroutine action.

  • Simplified error dialogs by using OK/ and :heavy_check_mark:︎Cancel Macro.

2 Likes

I've updated the OP with Version 4.0 of Insert Action to Check Variable(s).

  • Multiple actions (contiguous or otherwise) can now be selected and queried.

  • After the new action is created, it is selected in the editor.

2 Likes

I've updated the OP with Version 4.1 of Insert Action to Check Variable(s).

  • In the AppleScripts that add the new action, disabled Failure Aborts Macro.

  • In several actions (If Then Else, Switch/Case, While, Until, Pause Until) extracts the object when it is a calculation (e.g., local_B when local_A = local_B).

Thank you, @noisneil, for bringing the oversight (second bullet) to my attention.

1 Like

I've updated the OP with Version 4.2 of Insert Action to Check Variable(s).

  • When macro completes, the initially selected actions are reselected (thanks to some guidance from @ccstone).

  • Updated the TESTED WITH information.

1 Like

I've updated the OP with Version 4.3 of Insert Action to Check Variable(s).

  • Bug fix: Previous versions would capture variable values that were not variables (i.e., not \%Variable\%.+\%).

  • Updated the TESTED WITH information.

1 Like

I've updated the OP with Version 5.0 of Insert Action to Check Variable(s).

  • Bug fix: Since v4.1, captured all calculated objects as a single variable. For example, a simple calculated object of 100 would incorrectly be assumed to be a variable. (Thanks to @noisneil for reporting that issue.)

  • Captures multiple variables within a calculated object, e.g., (local_ABC + instance_XYZ) / local_JKL.

  • For the triggers, changed the modifiers: 1) ⌘: Display format for Short Values or Longer Values, 2) βŒ₯: Display Variables and continue or display and pause, 3) ⇧: Add the action Below or Above the selected actions. Previously, the order was βŒ₯, ⇧, ⌘

  • Changed the display and pause to use a Display Text action and a Pause for User Input action.

  • Added the KNOWN LIMITATIONS section in this header comment.

  • Updated the TESTED WITH information.

3 Likes

I've updated the OP with Version 6.0 of Insert Action to Check Variable(s).

  • Variables within calculated objects are now captured

  • Sets the size and position of the Display Text windows. As new windows appear, they are slightly offset on the x-axis.

  • Window includes a Window Number.

  • Plays the medium-volume Bottle sound after the action is inserted.

  • Optimized the search of XML for the variables.

2 Likes

I've updated the OP with Version 6.1 of Insert Action to Check Variable(s).

  • Changed the default to Display Variable(s) and Pause.

  • Corrected a typographical error in this header Comment and the Prompt With List.

1 Like