Can I create a "structure" type of variable?

“Find Image on Screen” returns a variable that is a Structure - it has multiple properties like .x and .y, etc. Is it possible for me to create my own structures?

If not, is there a recommended way to pass more than one parameter when I use the “Execute a Macro” action?

Thanks.

No.

Variables are simply strings. Always. Plain text strings and nothing more.

Keyboard Maestro has to deal with things like points and rectangles and so Keyboard Maestro allows a string that consists of a sequence of numbers separated by commas to be considered points or rectangles as appropriate.

Variables in that format can be referenced as arrays, as in “Var[3]”. So if Var holds “10,20,30,40”, Var[3] would be 30.

To simplify the use of rectangles and points, there is also a dot notation where you can use Var.top which is exactly equivalent to Var[3]. The possible values are:

  • x (1)
  • y (2)
  • left (1)
  • top (2)
  • width (3 or 1 - depending on whether there are 2 or 4 elements in the array)
  • height (4 or 2)
  • right (Var.left + Var.width)
  • bottom (Var.top + Var.height)
  • MidX (Var.left + Var.width / 2)
  • MidY (Var.top + Var. height / 2)
  • fuzz (5)

But they are all internally defined, you cannot define your own.

To pass more than one parameter, pick a character (or string if necessary) that is not used in your text and concatenate them that way.

Comma’s obviously work for some simple cases. If you text needs comma’s in it, then perhaps bullets (Option-8 •). Failing that, you can use some unique string like “,KMSEP,”. Use the Search Variable acton to break the parameter apart.

Or alternatively, just define the input variables. All variables are global. And yes, in programming, global variables are generally bad and all that, but we’re not building operating systems here.

“we’re not building operating systems here” LOL!!! Thank God! And no peer reviews either. Whew!

So, just to clarify, if I do use commas, then I can use the “Var[3]” method. I’d only need to break them apart using some other method if I didn’t use commas. Right?

FWIW, for the purposes of KM, I don’t mind global variables. Just looking for the best way to do things.

Thanks!

If you are happy to use Execute JavaScript actions, you can use Keyboard Maestro variables which are JSON strings.

You can then use JavaScript's built-in JSON.parse and JSON.stringify functions to decode and encode clusters of key-value pairs:

That’s actually quite an interesting idea. I may not use it for parameters (then again, I might), but there’s something else I’m doing for which it would be perfect!

Thanks!

1 Like

Only with numbers and only in a numeric field.

For text, you need to break them apart by some other means. I could probably support a syntax like:

%Variable%Var[5]%

but I don't currently.

There is a forum post (How to access a text array variable) that may also be useful.

1 Like

Thanks for the clarification. It makes more sense now.