Getting Started with For Each Collections

Hi,

I'm trying to make a basic macro that will create a custom number of variables based on an inputted number, but I'm struggling with how to get a for each loop running properly.

In its most basic form I can't get the loop to iterate past 1. I think I don't really understand what I'm supposed to do with the collections section, or what that area is really about?

Can someone help me understand what collections are for, or how I could get a macro like this to count past 1?

Your third action has a warning in the upper right. Click on that and it may tell you what is wrong.

Okay, so the direct reason your particular For Each Action is not getting past 1 is that you have written the Variable Number in the "to" field as %Variable%Number% instead of just Number
That particular Field is a Calculation Field rather than a Text Field so, Variables entered in it do not use the % notation. The clue is that when you begin to type in that field a "c" for calculation appears on its right side.
Fields with a "c" or "v" (for Variable) do not use the % notation. That is only used to designate Variables in Text fields that have a "t".

So, your Macro doesn't count past 1 because it is counting from 1 to some unknown thing, rather than from 1 to a number.

The collections step through different sequences of "things" one after the other, depending on which type you choose.

In your Macro you have chosen a Number Range collection from 1 to 3. So the Action will loop 3 times and count from 1 to 3.

At the top of the Action it says "For each" and you have a Variable called "NewVar". So, on the first loop "NewVar" will be set to 1, on the next loop "NewVar" will be set to 2 and on the third loop "NewVar" will be set to 3.

So, in your Display Text Briefly Action you could display %Variable%NewVar% instead of %Variable%Count% and you would still have a sequence of Variable values from 1 to 3.

To make things clearer, you could use the name "Count" for the For each Variable instead of "NewVar".

Here is putting all those things together. I added in a pause simply so you can see the counting happening, otherwise it happens too quickly.

EXAMPLE Count 1 to 3.kmmacros (3.1 KB)

The other "Collections" work with other selections of "things". For example the "Finder's selection" Collection will step through whatever files/folders you have selected in the Finder one at a time.

image

In the above case, the For Each Action will step through each item selected in the Finder one at a time (allowing you to apply some Keyboard Maestro Actions to that selected item) and on each step it will also set the Variable "Local_File" (or whatever you have called the Variable) to that item's full path and filename and extension, which can be very useful.

Also, that warning triangle:

That is there because you didn't make any use of the Variable "NewVar". The warning is more for your info than anything serious. i.e. a For Each loop has to have a Variable name written in that field otherwise it won't work but it is optional whether you use that Variable's stored data for anything.

Thank you very much for this detailed and thorough explanation, you addressed the root causes of my mistakes in detail which I really appreciate!