How Do I use Variables in Found Image Area Specification?

The two macro sequences below should (I think!) produce identical results, but one successfully matches the image and the other does not. The only change is whether I use a variable or a constant to set the search area. In both cases, the search area is exactly the dimensions of the front window and, yes, in both shots the same window is at the front:

Variables work fine in specifying the area, but you have to use them properly:


When using a Variable in a numeric area, do NOT use the Variable Token syntax --just use the Variable name, like:

See Variables in Calculation Fields .

1 Like

Thanks for the tip, but it's still not doing what I expect. Check out the image below. Notice that it still fails when I use the variable (applying the syntax you suggested) but works when I use the constants.

So, after a bit of experimentation I've discovered that the Pause Until works with the SearchArea array when the macro is executing, but not when it's doing its evaluation while editing the macro.

What's happening is very opaque and required trial-and-error on my part. Suggestions to @peternlewis:

  1. When using a variable in a Pause Until, show how the variable is evaluating in addition to showing that there is no match. E.g., "currently false" provides a bit of information but I would have been able to figure this out on my own if it had shown me how my variable references were evaluating so that I could understand WHY it was false.

  1. It would be great if KM could have detected my incorrect use of the %'s in my variable. It's still not entirely clear to me when I should/should-not use the %'s. Could KM generate a warning or a hint? I imagine this is a pretty common mistake.

Where possibly, I show evaluations, but it is simple not possible to show them everywhere.

The Value Inspector is useful for this, you can inspect values there.

Or you can add a disable Set variable to Text action which can show you the value in the action.

See: Text Fields.

You need percents in text fields - this makes sense if you think about it, since text fields contain text, and how could Keyboard Maestro know what is a variable or a token if you don't indicate it in some way?

Calculation fields (which is anything that expects a number, not text), use calculations. Since it is a calculation, anything that is text must necessarily be a function, operator or variable, so it is not hard for Keyboard Maestro to determine what is what.

As far as indicating, as much as possible, Keyboard Maestro shows invalid calculations in red or orange (orange if the calculation is invalid because a variable has a value that is not currently a number, but could be valid if the value of the variable changes). This does not appear everywhere yet, and it somewhat hamstrung by the way the system treats text fields. But if you actually just type those in to the fields, they should show up red, and indeed if you switch in to the macro, they will show up red:


Thank you, Peter. The %'s basically act as an "escape" in text fields; now i get it.

I did notice the color change when doing the entry and I didn't know what it meant, but as soon as I tabbed out of the field it went to normal colored text, which made me think that KM thought the error had been resolved. A more persistent error mark (and, even, a hover-over description of the error) would be helpful.

Thanks for all the help, Peter as well as to everyone else in the forum.


1 Like

Yes, unfortunately, for very very old historic reasons, text fields on the Mac are really complicated. There is actually only a single text editor for all the fields, and the system moves it around as you move around, and so it produces all sorts of behaviours like this which require working around. I am looking at it now to see if I can improve it, but I'm not sure if I can.