Proper Syntax for Array of Arrays?

Continuing the discussion from Proper Syntax For Accessing Array Elements Of A Variable:

I’d like to set up an array of coordinates (an array of short arrays):
(0,0)
(125,175)
(356,753)
(456,853)
etc…

I have three questions:

  1. This is a constant array (for my purposes). Can this be set up in the Preferences Default and if so, what is the proper syntax to use there?
  2. What is the proper syntax to use to set elements of this (array of short arrays) on the fly in code?
  3. What is the proper syntax to use to access elements of this array of coordinates in code?

But this could also apply to a slightly more complex array of arrays, for example, an array of rectangles:
(0,0, 10, 200)
(15,15,25,100)
(35,100,400,600)
etc
Any substantial changes to the above three questions?

Hey Dejah,

You can't set up a 2-dimensional array in Keyboard Maestro - only 1-dimensional arrays.

But one can work with that.

-Chris


Accessing a Two Dimensional Array.kmmacros (2.1 KB)

1 Like

Oh, man, that is Kludgy AF.

Could be. But you have to remember Keyboard Maestro is not a traditional programming language. @peternlewis calls it a "visual programming language".

So, it is somewhat like building a flow chart where are you drag/drop symbols.

I guess you could say it is primarily designed for non-programmers, for users who don't know how to program, but just want to get some routine task automated.

But, Keyboard Maestro also offers very powerful tools/actions for programmers. If you need serious array handling, then you are probably better off using a JXA or AppleScript (JavaScript is the easiest, most powerful, IMO). Through the use Execute Script Actions you can use these scripts.

Just to be clear, Keyboard Maestro has only one type of variable: string/text
It does NOT have true array or list variables, even though in some cases you can simulate the use of a text variable as an array.

Keyboard Maestro does not support two dimensional arrays.

So there is two possibilities, you could have a one dimensional array and do the maths yourself.

Source[(Row-1)*2+Col]

For an array of rectangles, the "2" would become "4".

Alternatively, you can use the new feature of Keyboard Maestro 7.2 to allow text arrays with arbitrary separators. You cannot, unfortunately, use linefeed as a separator, but you could do this:

Note that the first Set Variable is to Text and the second is to Calculation - they are using entirely different concepts of arrays.

1 Like

Nice. I think I read that in the change list and promptly forgot it.  :smile:

Another way to do this is to use AppleScript. (Test in the Script Editor.app.)

-Chris

------------------------------------------------
# Setting up your array for you.
------------------------------------------------
tell application "Keyboard Maestro Engine"
   setvariable "two_D_Array" to text 2 thru -2 of "
0,0,10,200
15,15,25,100
35,100,400,600
"
   setvariable "arrayRow" to 3
   setvariable "arrayItem" to 4
   
end tell
------------------------------------------------

set theDelimiter to ","

tell application "Keyboard Maestro Engine"
   set two_D_Array to getvariable "two_D_Array"
end tell

set two_D_Array to paragraphs of two_D_Array

set AppleScript's text item delimiters to theDelimiter

repeat with i in two_D_Array
   set contents of i to (text items of i)
end repeat

set returnArrayItem to item 4 of item 3 of two_D_Array

------------------------------------------------