If variable content = variable content

Hi there,

This is a very basic question and I think its something fundamental in KM that Im not understanding.

I have a variable "fill" and it increases in a 'for each' action to say 4. When it has its final number I save that final number to a new variable "value save"

so at this point
tabs = 4
valuesave = 4

I have a 'repeat' action (the photo below) that runs "fill" times. However when it gets to the last action within that repeat action I want it to stop and not run that last action if the values "tabs" and "valuesave" are equal. Ive tried the different match options with %Variable% and without on many different options and cant seem to get this right. I tried this also with an 'Until' action and just ended up creating an endless loop of tabs :confused:

Hi Billy,

You're definitely not alone in having trouble wrapping your head around the way variables and loops work. I went through a very similar struggle before things started to click for me (and I'm still far from an expert compared to some of the macro and scripting masters here :slightly_smiling_face:)

Let me see if I can explain where your macro is going wrong. First, in your current repeat loop, nothing is changing in each loop that could affect the "end loop" If/Then condition. With the actions you've shown here, the "fill" variable's value will remain unchanged at 4, so the intended If/Then condition will never be fulfilled. For things like this you need to increment a counter or otherwise change the variable that will be used in the If/Then to determine if the conditions you want to check for have arisen.

Second, I can see you've been led astray by If/Then's admittedly confusing-at-first myriad of "If variable…" options. The "match/does not match" options are specifically for regex matches, not plain text or calculations, so they can't be used to see if one variable with a number value is the same or different then another variable with a number value. In the case you present, I think you would be better served by one of the calculation-specific options, is <, as those are explicitly designed to work with number values.

One last pitfall I'll point out that is especially easy to miss is the variable syntax that KM expects depending on the kind of field. Do you see the little T at the right of the "does not match 'valuesave'" field? That T and the other similar V and C badges are a new addition in KM8 to make it clearer what kind of syntax to use. V and C fields, which stand for Variable and Calculation, expect variable names to be used as-is (tabs, fill, valuesave, etc), while T (for " text tokens") fields require that variables be surrounded in %, and preferably (for greater reliability) prefixed with %Variable% as well. There's more information on the Variables wiki page here: https://wiki.keyboardmaestro.com/manual/Variables (just ignore that it currently mistakenly shows V for text token fields as well as variables :slightly_smiling_face:)
Essentially, this means that you need to pay attention to what kind of field you're writing in if you want variables to work as expected. Right now you only have valuesave in the "does not match" field, which is a text token field (as indicated by the T at the right). Putting aside the fact that "match/does not match" is for regex and not numbers, if you wanted the condition to match valuesave's value here, it would have to be written like %Variable%valuesave%. I hope this was all clear; I know it can be confusing when you're still getting learning your way around KM!

Finally, here's an image of a sample action that I believe should work as intended for what you describe. Actions that I've added or changed are marked with different colors or otherwise circled in red; the rest are the same as what you showed. Please also note that valuesave is still written as valuesave here instead of what I just showed you, %Variable%valuesave%, because the option that I'm using, is <, uses a calcuation field rather than a text field. As always, feel free to ask if anything is unclear or you have further questions!

1 Like

I literally slapped my forehead after reading this. How did I miss that?!?!?!?! hahaha.

Thank you this definitely clears up some stuff! I dont know how I missed that in the box.

Thank you so much again! This clears up many things for me.

1 Like