Easiest way to align 2 collections

What would be the easiest way to align 2 collections like:

First item
Second item
Third item



Leading to the desired result:

First item=Bird
Second item=Horse
Third item=Plant

Thanks in advance!


I don't know about the easiest way, but here's one way to do it:

[Example] Align Two Collections.kmmacros (4.0 KB)



Thank you very much!

I’ll use it for adding several term pairs simultaneously to a CafeTran glossary: https://cafetran.freshdesk.com/support/discussions/topics/6000051642



Well, easiest is definitely in the eye of the KM Macro builder! :wink:

I actually developed two macros for this:

  1. Using all non-scripting KM Actions, using the KM Variable Array
  2. Using AppleScript.

and I have to say that, in my case, AppleScript won hands-down.

Also, in this case, I think the AppleScript is simple enough, readable enough, that most end-users could modify it to suit their needs. But you tell me: how understandable to you find this AppleScript, even without any comments?

### Requires Keyboard Maestro 8.0.3+ ###

set kmInst to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine"
  set kmList1 to paragraphs of (getvariable "Local__List1" instance kmInst)
  set kmList2 to paragraphs of (getvariable "Local__List2" instance kmInst)
end tell

set kmListCombined to ""
repeat with i from 1 to count kmList1
  set kmListCombined to kmListCombined & (item i of kmList1) & "=" & (item i of kmList2) & linefeed
end repeat

return kmListCombined

Here's the very simple, 4 Action KM Macro:

###MACRO:   Combine Two Lists Using AppleScript

~~~ VER: 1.0    2017-10-25 ~~~

Combine Two Lists Using AppleScript.kmmacros (3.8 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

1 Like

Here's #1, using the KM Variable Array, which is a great feature (but not well documented -- I'll try to fix that).

I want to first highlight the part of the Action that uses a KM Variable as an array:

First, the "array" is actually a comma-deliminted list, like this:

which I built in the 3rd Action:

by replacing linefeeds with commas.

Each element of the array is separated by a comma.

But the hard part (actually just unclear part), is how to reference each element of the array. While the KM Wiki shows us:
%Variable%<VariableName>[3]%, where 3 is the index in this example.

it may not be clear exactly what is meant by "can be any Calculations."
Simply put, that means you can use another KM variable as the index, which did. I used "Local__i":

So, as we loop through the For Each loop, Local__i gets incremented by 1 each time.

Make sense? If not, please ask questions.

OK, enough documentation. Here's the macro:

###MACRO:   Combine Two Lists Using KM Variable Array

~~~ VER: 1.0    2017-10-25 ~~~

Combine Two Lists Using KM Variable Array.kmmacros (5.0 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


One reason I prefer the AppleScript, is that I find Actions like this very hard to read:


Everything just runs together, and there are too many % symbols to sort out.
I would have preferred something like this:
Local__Line + "=" + Local__List2[Local__i]

IOW, Everything that is NOT in quotes is assumed to be a Variable or function.
But hey, that's probably the coder in me talking. Those of you who haven't coded may very well prefer it the way it is.
I'm not complaining, really, just explaining another reason why I prefer the AppleScript.


Good luck and enjoy whatever method/macro works best for you. :smile:

1 Like