How Do I Use %TriggerValue% in If Then Else Action?

I’m passing a parameter from JXA to a macro using doScript.
doScript(“Macro Name”, {withParameter:“1”})
How can I use that parameter in if then else action?
I tried
if the variable %TriggerValue% contains 1
If the calculation %TriggerValue% returns true
It doesn’t seem to work.
I feel like I’m missing something simple, but I couldn’t get it to work.
I’d appreciate your help!

Hi @Chi,

Since %TriggerValue% is a token, not a variable (although you’re far from the only one to get them confused) what you want in this case is a text condition:

if the text %TriggerValue% contains 1


Wow, that worked!
Thank you very much!
Yes, token, variable, calculation seems little confusing.
I need to read the manual again more carefully.

1 Like

One follow-up question to this, I am using an "If then else" statement for a very crucial information, basically it auto-confirms something if %TriggerValue% is specific date using a "Text" regex "matches" condition in If,then,else eg

Basically cannot afford any form of error/confusion, that's why I am not using Set variable command because I don't want it to retain a "variable" if somehow macro fails at some step.

It seems to me TriggerValue would be a safe value to check right?
(as it is created fresh on each new Macro trigger. I will be triggering via URL containing date as value)

Would love your inputs as well @JMichaelTX (PS: huge fan of your tips)
Would appreciate any inputs on the "safest full-proof way" of doing this.

Thanks in anticipation

I'm afraid it's hard to comment on the safest fool-proof way of doing this because you haven't yet told us exactly what "this" is, but as long as it's matching correctly, TriggerValue should certainly be safe to check for your purposes if you're concerned about retaining erroneous data every time the macro is run (though note that you can also use local variables to ensure fresh data is used each time, since their values aren't retained across executions either).

1 Like

Ah thanks for that.

Sorry. "This" was run a macro only if the triggervalue extracted from a webpage equals to the regex matched dates as shown in image. :slight_smile:

Also, slightly off-topic but is there a way to display a warning that a macro is running?

  • I mean I can kind of see that macro is running by dropping down into KM menu in the top menu and listed in the Cancel section
    • but it would be great if I can have a warning/overlay/red bar on top of screen that macro is running.
      • would be pretty cool if maybe something like how iPhone shows a blue bar when some app is using Location service.

Thanks again for the help.

You're welcome, and no worries. Assuming you've tested your macro with test data and ensured it will match the dates extracted from the webpage in question in the way you want, it sounds like you should be good to go.

For your off-topic question, the easiest two ways I can think of to tell when a macro is running are:

  1. Use these icons for the KM menu bar: Iconaholic Loud

  2. Add the KM Engine to your list of applications accessible in KM, then display text in a window when the macro begins and have KM close it when the macro is done:

Example Macro.kmmacros (4.8 KB)

Oh nice. Thanks. But seems like this still is prone to error if macro fails in between somewhere.
Basically I want to display text in a window whenever it's running and not display it whenever it's inactive.

Basically can I have a "%ExecutingMacro%" like thing work similar to hiding/showing palette on applications.
I mean my palettes of specific apps seemlesly close and open on the corresponding apps, I would like to achieve that same level of logic/confidence. That whenever I see the "warning text", I know the Macro must be running, and when it's not there I know it's not running.
Especially because the macro has long timeout periods.

In that case, I'm afraid that's beyond the scope of help I can offer; I can't see any good way to make an alert or window with KM actions alone that is only visible when a certain macro is running, and disappears automatically when it's not even if the macro fails before completion. In the meantime, the best I can I do is point you towards the first option I suggested again; using a loud version of the KM menu bar icon so it's much more obvious at a glance when a macro is running.

I've been able to get this to work sometimes, but not others. For example:

I wonder why this is...?

Something I do to avoid the problem you're experiencing is this: set a variable to the value of %TriggerValue% and then use that variable subsequently, as in this example:

KM 0 2021-07-27_13-58-18


There are some tricky concepts in this post so someone might try to correct me.

The %TriggerValue% token exists within the running macro when the macro is called. When you are looking at a macro in the editor, that's not the running macro. How could it be, since you could have multiple copies of the same macro running at the same time? It would be physically impossible to display multiple values of the same token at the same time. Therefore in the editor that token is always "empty." It can't be anything but empty because the editor is not associated with any running macro. Think of it this way: when you run it, a copy is made, and you don't get to see the data specific to that copy. In fact, if you're very observant you will notice that if you make a change to a macro and then trigger that macro too quickly (typically under 1 second), your change isn't included in the running macro because the "copy" didn't have time to be created in that one second. I'm not sure if anyone should call that a "bug" but it's a feature I'm not too crazy about. One way to avoid that problem is by triggering the macro by pressing the Try button on the Group containing the entire macro - that forces a copy to be made before it runs.

I used to struggle with that idea too, and so what I used to do to help myself is what tiffle explains above. But his idea suggests using a Local variable, which provides the same problem as the token, since Local variables are also associated with the running macro and therefore cannot possibly be associated with the macro visible in the editor. So I used a global variable. That helped to me understand my code better. But eventually you will get used to the idea that the Editor is not directly associated with any running code.