Execute "This Macro" and update variable

Can I use the "Execute Macro (This Macro)" action and send a variable value to the second instance? Example:

My first action is the prompt. Since I haven't set the Local__test variable, it shows nothing.

After the prompt, that variable it then set to testing.

When it executes this macro the second time, the prompt now shows testing in the prompt section.

:warning: I don't want to use subroutines, global variables, or any extra macros.

I'm just curious to know if I can achieve that just using a single macro? I'm not very familiar with how the "With Parameter" works other than when using Subroutines, so I'm not sure if that is useful here?

If you click the gear icon in your Execute Macro Action and choose "Help" it will take you to a page that explains how this works:

action:Execute a Macro [Keyboard Maestro Wiki].

So, in your sub Macro, you would access the data you have sent over using the Token %TriggerValue%

To test this make a Macro that has a single Action "Display Text" and have the text to display as %TriggerValue%

Then make a Macro that calls this sub macro, with a parameter. Just put some test text into that field (or a Variabe as you have done).

When you run the calling Macro, it will execute the Sub Macro and the text you have sent over will be displayed.

I know how it works with a sub macro / sub routine. I wanted to avoid multiple macros if possible. Maybe not?

But if you are using an Execute Macro Action that is running another Macro. I don't understand what you are after? You were asking how the With Parameter works when calling another Macro.

Are you wanting to simply position the Next Engine Window? In that case you can do that in your single Macro without needing to call another Macro or Subroutine.

I'm not executing another macro. I'm executing the same macro (hence This Macro). I mean, it's another instance, so it's like another macro, but I'm actually triggering the exact same macro. The name you see "Set Next Engine Window Position" is just because I haven't renamed the macro and that action is the first one, so it automatically named it "Set Next Engine Window Position"

Example:

Let's say that I have a macro with a prompt for input. After that I will have an action that checks if that input is valid. So it is valid, all good, it keeps running the macro. If not valid, I want to run the same macro, but this time I want to add a message in the Promp section of the Prompt action that says "XYZ is not valid. Try a different one".

With a global variable, that would be easy: I could have an empty global variable by default. When it runs the macro, since the variable is empty, when I use %Variable%MyGlobalVar% it will not show anything.

Then, once it runs the prompt, if the input is valid, it does nothing to the variable, if it's false, changes the state of the variable to include the message "XYZ is not valid. Try a different one", so when I run the macro again using the Execute Macro (This Macro), it will now show the message. Then, once the macro fully runs based on valid input, I can set the global macro to empty again.

My goal here is more about understanding the limitations of KM when it comes to using a Local variable if I run the exact same macro. I understand that local variables are exclusive to the macro instance running at that time, that's why I was wondering if the "With Parameter" could somehow work differently, more like a global variable, but without actually setting the global variable. Which probably can't.

Now that I'm thinking about it, maybe I could emulate this using the "Until" action. When it runs the first time, it sets a local variable to the message, then depending on the input being valid or not, it goes back to the beginning of the loop or continues with the macro. Gotta try it.

@Zabobon
I was able to achieve it with the While action. I tried with the Until action and it also works as expected.

Exactly what I wanted. No global variables, no extra macros.

In this case I'm using the literal word valid just to make it easier to test it, but it could be a simple check of something being true or false.

Using While

Check input validity.kmmacros (23 KB)

Using Until

Check input validity.kmmacros (23 KB)

You posted the solution before I could reply, but that's exactly what I was going to suggest. I use this construct a lot, and you can do nice things like having different messages for different invalid input, or even changing the input prompt (by running a loop in a loop, which gets a bit confusing) based on an input value.

-rob.

1 Like

Exactly! I've used that in some macros. Super useful!