Creating variables from Prompt for Snippet (v11) action

I love the new Prompt for Snippet action in v11. It's got a number of uses, including editing multi-line variables, and obviously, collecting a chunk of text for use elsewhere.

But I wanted to use it for something else: As a simple multi-variable input form. Yes, you can do this with the Prompt for User Input action, but maybe you want more text around the inputs to help the user understand what's being requested. Or maybe you have a ton of inputs, and setting up a long Prompt for User Input action can be time consuming.

The attached demo macro uses a Prompt for Snippet action to create multiple variables from the user's input.

Download Macro(s): Assign variables to snippet inputs.kmmacros (8.7 KB)

Macro screenshot

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.6.4
  • Keyboard Maestro v11.0.2 required

So given this snippet action's inputs...

The outputs would be four variables holding Joe Smith, Designer, 7, and My current Mac is 14 years old and too slow to do anything.. The attached macro does just that by looping through each line of the snippet result. Each row is analyzed to see if it contains a "variable identifier," in this case a colon-space combo.

If a line has the identifier, the two parts are split into a temp variable name and its associated value. As an example, one input, Years Employed, is renamed so that the variable name doesn't have a space in it. If a line lacks the identifier, it's simply skipped.

After creating the variables, a dynamic variable assignment creates new local_ variables for each temp variable, set to the associated value from the snippet form:

You can see I've inserted the local_ prefix before the TBD variable; this could just as easily have been instance_ or left out completely to create global variables.

At the end, all four variables are displayed:

I may not use this very often, but being able to split out snippet input into named variables will come in handy at times.

-rob.

6 Likes