Can I 'Call Back' to a Defined Action Sequence Multiple Times in KM?

Hi there, I am trying to create a macro that repeats a specific set of actions a number of times. It's maybe about 15 actions per set, but within each set, I need 1-3 of the actions to change each around. For instance, one of the actions is an "insert text by typing" action, and I need this text to be a new string each round, but the preceding and following steps are identical between the rounds.

I am wondering if there is an efficient way to have KM 'memorize' the repeated steps that are unchanged each round.

Part of the intent is of course to reduce the visual clutter, but another intent is also that, if say I need to adjust one of the non-unique, repeated actions, I only have to do it once and not 15 times.

I suppose I could make 'sub-macros' for these (as described here), but for cleanliness, I'm apprehensive about creating fragmented macros just for this utility purpose, and I'm wondering if there's any method to acheive this within a single macro (where the macro refers to / calls back to an aforementioned set of actions).

Yes, if you use user input and save it as a variable, it will save it as the default. There is a way to do it.

Interesting - I am new to this, could you provide an example of its use?

Memorize? You mean you want KM to observe actions from the user when the macro runs and then repeat those actions? How would KM know when the user's actions are completed? I think you should provide a specific example, showing key-by-key what you want accomplished.

The usual way to do this sort of thing is with a loop, holding your changing data in variables and/or varying your actions using conditions.

That's a bit wafty -- but the specifics depend on what you are trying to achieve. You could, for example, put each bit of changing text in a line of its own in a variable and then "For Each" the lines. So this:

image

...will give you:

...when run with a text editor frontmost.

I'm afraid it's difficult to give a more useful example without knowing what you are trying to do. But start by writing down what you want to do, then looking for patterns and differences. That'll help you work out a logical flow.

1 Like

Thanks for this reply! I've attached a copy of my full macro so you can see what I am trying to achieve.

For context, it is creating several tracks within my Logic Pro setup. The unique actions are the instrument group selected ("type the ^F4 keystroke"), the instrument searched ("Insert text "CSW Solo Flute Legato" by typing") and at times the track delay input "insert text "-222" by typing"). All other actions - and you'll see there are a lot - are identical between rounds, and take up a lot of space in the sequence.

Load Full CSS Orchestra.kmmacros (268.8 KB)

I didn't examine your entire macro, but it sounds like you just want to repeat some common actions a number of times between groups of unique actions. Well, the following will work, and I do this sort of thing all the time.

image

This is super helpful—thank you! I'd love to take it one step further if possible. Ideally, I’d like to define all my unique actions in one place up front, almost like a grid of inputs.

For example:

  • Round 1: Unique01 = A, Unique02 = B, Unique03 = C
  • Round 2: Unique01 = D, Unique02 = E, Unique03 = F
  • Round 3: Unique01 = G, Unique02 = H, Unique03 = I

This would make it much easier to visualize and update the unique material without having to rebuild everything inside the macro.

Is it possible to set this up so I define everything in one spot, and then have KM automatically pull those values into the switch variable actions?
Load Full CSS Orchestra UPDATED.kmmacros (54.0 KB)

Oh, um, I'm not sure. Let me think. When I wrote that code, I didn't realize you had multiple blocks of unique actions; I thought you had only one.

Yes, I think I can do it, but I don't think it really benefits you much, because it's still the same amount of code. It may take me an hour or two, as I need to make a meal now.

In your case, some of your switch variables contain hotkeys. Hotkeys are not cable of being "variablized" the way you want to do it (at least not without help from AppleScript, which I try to avoid.) So for this attempt I'm going to use a modified approach. Consider the following approach. This will put all your common code at the front of your macro, making the bottom half of your macro all the rest of the code. My approach uses a sneaky technique of making the macro call itself with a parameter for each block of common code. So this might make you happy, but I'm not sure. I think there may be another way to do it that is even closer to what you want, and I'm thinking about that now.

Since you were successfully able to adapt my first suggestion into your own code, I think you will be able to adapt this suggestion into your own code too.

P.S. In case it isn't obvious, you can place a hotkey as another trigger to the above macro, which is how you yourself would call it.

Stepping away from @Airy's suggestion to a more general approach, the usual way to do this sort "repeat the same actions with a bunch of different values" is to work over an array containing those values. In pseudocode:

set myArray to [1, 2, 3]
repeat with eachItem in myArray
   print eachItem
end repeat

...would print 1, then 2, then 3.

When, as in your case, you have groups of things to process you use an array of arrays which for your example would be set up as:

[
   [A, B, C],   
   [D, E, F],
   [G, H, I]
]

Unfortunately, KM doesn't do "proper" arrays (unless you add the complications of JSON/JavaScript or similar). But it does have a "For Each" action that will process text line by line. And it has "psuedo-arrays", where text is split into elements delimited by character(s) you determine -- the trick is to pick a delimiter that doesn't appear in your actual text, and in your case a tab character is probably suitable and also makes the text block easy to read and edit.

Using your example again:

Loop Demo.kmmacros (4.1 KB)

Image

You can't use a variable in a "Type a Keystroke" action, so you'll need to use an "If... Then" or a "Case/Switch" action to determine which key should be pressed.

Putting the above together, and using eg "C6" to indicate the ⌃F6 and "S6" for ⇧F6:

Load Full CSS Orchestra (Loop).kmmacros (39.7 KB)

Image

I've taken you at your word and not checked that it is only three actions that can vary. And this has not been tested in Logic as I don't have that app. With the explanation above you should be able to fix any problems, but shout if you get stuck.

WOW! This is nothing short of amazing and completely addresses what I was going for! Thank you for introducing me to this action! And the example macro you provided works great!

Or, thirdly, you can use an AppleScript action, without IFs or Switches. In basic form, it looks like this:

image

AppleScript uses key codes, not Function Key numbers, and Apple's key codes are not numerically ordered. Even so, you don't need conditional statements to handle that. All that's needed is to create a variable containing all the key codes for, say, F1 to F12, like this:

image

From there, you just index into that variable with the number of the function key you want to press. You pass that variable to the AppleScript action above, and the work is done. "No Ifs, Ands or Buts."

Not quite -- OP also wants to use modifiers. If you're really going to avoid ifs then you'll have to include those, either expanding your key codes list or by having an extra "modifiers" field in the original list and processing the appropriate entry in your AppleScript.

None of which is really worthwhile. KM provides easy, simple, quick actions for doing what we want. Using an AppleScript instead makes things more complicated, obfuscates things, and is much slower.

I agree with you completely. More or less.