For Each Item - difficulty understanding action

I am understanding difficulty in understanding the For Each Item action. I understand the purpose of the action, based on the first paragraph of the Wiki help for this action:

The For Each action loops over a set of collections of items, setting a variable to each value and executing the contained actions.

but am having difficulty applying it to the description on the Wiki page or examples found on the user forum.

For example:

In the line that is “For Each [Variable] in these collections”. Is what you type for [Variable]

  1. to set as a variable the name you type (so that each item that follows based on the following line (‘The Finder’s Selection’, ‘The lines in’) has the value; or
  2. is a variable already set elsewhere or in a prior step?

I think it is 1.

Also I do not seem to see any explanation of what each of the Collections do (the titles may be obvious in some contexts), but not how an actual item works: i.e. an explanation of The Finder’s Selection, in the context of a macro is missing (whether you just have to a Finder window open, or a folder selected etc).

The example on the Wiki seems quite complicated (and those posted in the Forums), and all seem to assume what the first very step of “For Each [Variable] in these collections” is.

Would it be possible for the Wiki to have some more basic explanation and a few very simple examples? I admit I am a very basic user but I am obviously missing something very fundamental here. My understanding of looping macros is limited to that found in the macro languages for WordPerfect and now FileMaker, which from my view point operate differently.

Any help would be gratefully received.

Hey Victor,

Think of a collection as a list.

A list of selected items in the Finder.

A list of lines in a variable.


The For Each structure is:

For Each <LineVar> in these collections:

Keyboard Maestro enters a variable named Variable as a default, and I always change it to something like LineVar or filePath for clarity.

The Finder Selection Collection works like this:

tell application "Finder"
  set myFinderItems to the selection as alias list
end tell

repeat with theItem in myFinderItems
  # Do something with theItem
end repeat

Except that the list contains Posix Paths rather than aliases.

A super simple example:

Generic-Test 01.kmmacros (3.7 KB)


Edited for clarity and to fix markdown-eaten-characters 2016/03/20 10:00 CST

1 Like

@VWKM, thanks for your feedback on the wiki.

I have made some changes to the Wiki that I hope will help clear things up:
KM Wiki: For Each Action

Specifically, I've made these changes in the "How to Use" section:

Please take a look and let us know if this helps.

Did you click on the link to the Collections page?
From there you can click on each individual Collection for details.

Can you give me an example of a repeat loop from FileMaker?
I'll translate it in to a KM For Each process.

Did you download and look at the example in the wiki:
MACRO: [FILE] Process Lines in Text File

It is very simple. It just processes line read in from a text file.