What would be the easiest way to align 2 collections like:
First item
Second item
Third item
and:
Bird
Horse
Plant
Leading to the desired result:
First item=Bird
Second item=Horse
Third item=Plant
Thanks in advance!
Hans
What would be the easiest way to align 2 collections like:
First item
Second item
Third item
and:
Bird
Horse
Plant
Leading to the desired result:
First item=Bird
Second item=Horse
Third item=Plant
Thanks in advance!
Hans
I don't know about the easiest way, but here's one way to do it:
[Example] Align Two Collections.kmmacros (4.0 KB)
###Results
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
Hans
Well, easiest is definitely in the eye of the KM Macro builder!
I actually developed two macros for this:
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 Library Combine Two Lists Using AppleScript
####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/6/678d88ef3fc77e2e476c34ca4966486f2ba6230e.kmmacros">Combine Two Lists Using AppleScript.kmmacros</a> (3.8 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**
---
<img src="/uploads/default/original/2X/3/3490fd55c898c57748b6bab2f39ed7dd55352b00.png" width="685" height="1059">
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:
%Variable%Local__List2[Local__i]%
First, the "array" is actually a comma-deliminted list, like this:
Bird,Horse,Plant
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":
%Variable%Local__List2[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 Library Combine Two Lists Using KM Variable Array
####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/8/82c104ff9f760e7014bae463cba19f2427b4c9bd.kmmacros">Combine Two Lists Using KM Variable Array.kmmacros</a> (5.0 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**
---
<img src="/uploads/default/original/2X/4/4da37de29c0cd3e9fe26c24ac78464935fba70e3.png" width="459" height="997">
---
### Footnote
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.
YMMV.
Good luck and enjoy whatever method/macro works best for you. :smile:
In its current state the script does not account for lists with differing numbers of lines. Do you think there would could be a simple addition to the script that could account for this or would the only solution be to count the number of lines in each variable and add the differing number of linefeeds to the one with less items?
I don't think it would be hard to make changes to the script. It just depends on how you want to handle the two lists having different number of lines.