As I mentioned in a previous post about KeyboardMaestro, even though I’ve barely got started with KeyboardMaestro, I was already able to create some pretty useful macros that simplify my workflow quite a bit. During my previous experience, I noticed an interesting thing, where the “Set Variable To Calculation” action does not always recognize valid calculation expressions as KeyboardMaestro actually supports. This is shown in the below screenshot.

Here, the “Awesome Variable” is first defined, but then the “+ 1” piece of the expression is highlighted. There is nothing wrong with this expression, though–if you display the value of the variable, it’ll say 2. Yet the highlight has been really confusing me.

In this forum post, the author explains that if the variable is undefined, then the variable itself (together with the subsequent expression) will be highlighted. In this case, the “old value -> new value” hint at the bottom of each action will (in an older version of KeyboardMaestro) be a red “invalid”. In current version of KeyboardMaestro, it will not be red, but simply be “empty ->”.

What appears in above screenshot is that, if we take the two “Set Variable To Calculation” actions as two assignment statements in a programming language, then the variable “Awesome Variable” is “defined”, yet the “+ 1” piece still cannot be parsed correctly. This seems to suggest that KeyboardMaestro parsing engine is interpretative in nature, that is, it doesn’t know about the validity/definedness of the variable until it has run it. Therefore it marks the rest of the expression “+ 1” as red.

What’s interesting then is what the below screenshot suggests.

Here, I ran above macro once, so that the KeyboardMaestro engine for sure knows “Awesome Variable” is now 2. It also knows this old value will be updated to 1 after executing the first “Set Variable To Calculation”. However, for the second “Set Variable To Calculation”, KeyboardMaestro does not know that it is immediately executed after the first, so that “Awesome Variable” will actually be updated to 1 when the second is executed, thus it mistakenly says that “Awesome Variable” is 2, and its value will be updated to 3 because of the calculation “Awesome Variable + 1”. This is why we cannot trust the “old value -> new value” hints at the bottom of KeyboardMaestro’s variable setting actions. One must exercise caution when “programming” in KeyboardMaestro.

Given this, it’s not hard to understand that in the second screenshot, KeyboardMaestro now knows “Awesome Variable” is 2, which is indeed a numeric value, so it renders the expression “Awesome Variable + 1” as valid (no red highlight). This seems to suggest that the KeyboardMaestro engine keeps an internal mapping of defined variable values, but not doing more behind-the-scene calculations than that.

So what are the take-aways here, about KeyboardMaestro variables and how KeyboardMaestro interprets / manipulates them? * By default, variables are of value “empty”, i.e., they are undefined. * KeyboardMaestro does not keep track of “empty” variables, and for certain does not treat them as valid numeric values. * An “empty” variable is still a valid variable, and can appear in an expression. Therefore the “Awesome Variable” part of the whole “Awesome Variable + 1” calculation is considered valid and not highlighted, in the first screenshot. * When a variable is not known to KeyboardMaestro to be interpretable as a numeric value, using it to construct arithmetic computations will lead to an unparsable expression, causing the “+ 1” part of the “Awesome Variable + 1” to be highlighted. * KeyboardMaestro does keep a map of defined variables to their values, provided that the macro defining the variables is run at least once. A variable simply appearing in a macro is not sufficient for the variable to be considered as defined. * KeyboardMaestro does not consider a sequence of “program like” actions as a piece of program, and does not evaluate them in order to show you the expected “old value -> new value” hints. It simply treats each action as its own, using the cached map from variables to their respective values at the moment to display the hints for each action individually.

So far so good, but how about having an undefined “Awesome Variable”, and setting it to “1 + Awesome Variable”? You’ve gotta try it yourself! I cannot explain whether this is a bug or a feature now, given the below screenshot.