Is there a way to simplify setting a "flag" variable based on a comparison?

Lots of times I want to do something like this:

if value < 0 then set flag to 1 otherwise set flag to 0

which ends up in KM looking something like this:


Is there some simpler way of doing this? Like a function that returns 1 or 0 depending on a condition like this hypothetical example:


where the function ISTRUE(some condition) would return 1 if true, 0 if not.

@peternlewis I don't think there is a way to do this currently, and if not, could you add it sometime in the future? Thanks.


I cheat and use the shell to do this quickly :slight_smile: ... probably not RAM efficient, but very quick to add as a saved action:

if [ "$KMVAR_Local_Diff" -lt 0 ]; then echo 1; else echo 0; fi

Save the results to Local_LeftFlag and off you go. Note: I fully agree with the feature request for a way to do this natively :).


1 Like

Try this:


It works only with math formulas (calculations) not things like "if an app is running."

It's called the Ternary operator, and it can even be nested or used in sequences. I use it often.

Moreover, in your example, a<b returns 1 for True and 0 for False, so you can just assign "a<b" to some other value in any calculation.


I have no idea how I missed this. I use that operator all the time in JXA / JavaScript. You rock!! (As does @peternlewis for including this, of course.)

Thanks a million.

1 Like

I had this post by @Nige_S bookmarked a while back

1 Like

You're welcome. It helps to balance out the universe, since you helped me so much.