Hello everyone!
Is it possible to check if a parameter has been passed by a "Execute Macro" action?
Say you want to make a subroutine that
a) gets a parameter passed and then sets a variable to %TriggerValue% or
b) doesn't get a parameter passed and then uses a fixed value for that variable?
For "Execute a Macro", you're almost there already. Since the parameter is passed as the trigger value, the called macro tests %TriggerValue%
and if that is empty it uses its default instead.
Actual subroutines have parameters passed into variables -- so you do the same except you check the variables, not %TriggerValue%
.
Passing a parameter isn't a problem. If I decide to not pass a parameter, the TriggerValue of the execute macro spills out the hot key I used
Sorry, but I don't quite understand what you are doing.
This will show you how to check if a parameter was passed or not -- try with and without entering something in the dialog:
Execute a Macro Demo Macros.kmmacros (5.7 KB)
If I've completely misunderstood the problem, upload your (suitably sanitised, if required) macros and explain what you are doing. Instructions for how to do that can be found at How to Post/Upload your Macros and Scripts
I think I see what you mean. Now: In your example uncheck "With Paramter" in your Main macro, you will see what I mean
I want to be able to Execute a macro sometimes with a parameter passed, sometimes not.
I think I understand. The %TriggerValue% value is not "erased" when a macro is called. Indeed you are right, it can inherit its value from the PREVIOUS macro or mechanism that called it.
I think you will need to convert your macro to a subroutine trigger, and in that case you can determine what you want to determine, because then you can avoid this mechanism. I remember a long thread about this topic years ago.
Alternately, you can just check if the TriggerValue token "looks like" a hotkey or not.
He's not passing variables to subs, he's trying to determine if the main macro passed a parameter or not using the parameter field of the Execute a Macro action. If you read the thread I cited, you will find out what the problem is.
Oh now I understand. It's also in the wiki. :
Although Execute a Macro is not technically a trigger, when a Macro is executed by this method (the “Sub-Macro”), the %TriggerValue% token (as evaluated in the “Sub-Macro”) will be either of two values:
- The %TriggerValue% of the calling (“Main”) Macro
OR - The value of the Parameter of the Execute a Macro, if a Parameter is provided.
But I cannot let a sub routine check on this, right?
A subroutine is guaranteed to be able to check if the parent's "parameter" is empty or not. That was what you are asking for. A macro can't, due to the behaviour described in the link above. So if a subroutine doesn't solve your problem, then I don't understand what the problem is.
Yeah maybe I didn't understand the difference between a subroutine and a macro!
Subroutines are (technically) macros, but they use a trigger called "Subroutine Trigger" and then you use a special action called "Execute a Subroutine" to call them. They are really a nice addition to the language, because they offer more features.
In particular, I love the ability to pass in an unlimited number of parameters. So you could write one "Move and size this window" subroutine, then call it each time with different values for the size and location, for example.
-rob.
Then always tick the "With Parameter" box, but leave the parameter field empty when you don't want the submacro to use a parameter.
Better yet, for a "self-documenting" approach, always tick the box and set the field to "intentionallyBlank" or similar, then test for that in the sub. If that sounds like a lot of work every time, remember that you can make an action like
..and add it to your "Favorites", ready to drop into your macros whenever you need it.
Yep -- got it now. "Can a macro determine if the action it was executed by had the 'With Parameter' box ticked?" The short answer is "No", the long answer is in the thread you linked to.
Even that's probably wrong (I'm full of wrong today, it seems!). I reckon you can infer, with a high degree of certainty, whether or not a parameter was passed. The uncertainty would be if the passed parameter was exactly the same as textual representation of any one of the parent macro's triggers...
Bit of a think required then, hopefully, demo to follow...
The Architect is an S Tier, but you're still an A Tier. Your communication skills are superb.
Proof of concept, works for (at least) hot key and typed string triggered macros, and could be taken further if the triggers were properly parsed rather than relying on their text description.
And the regex needs some work so it doesn't match if the parameter happens to be part of a trigger description...
Anyway, I think it shows how this could be done. I doubt that it should be done, though!
Was there a Parameter? Macros.kmmacros (7.8 KB)
Edit to add: For hot keys alone you could get by with
...as long as your parameter isn't "Key", "Trigger", "was"...