Browse the docs

Collect a list

Builds a list (files, folders, apps, windows, text pieces, numbers and a few other kinds) for a Repeat to go through.

Make a list of kind into into

Keyboard Maestro: For Each collections

Details #

Fills the variable named in into with a list. Pair it with Repeat using forEach and in to do something with each item. The step's output holds count, the number of items.

Choose what to list with kind:

  • files and folders: the contents of the folder in path (~ works). Sorted by path. recursive goes into subfolders, hidden includes hidden items, and includeFolders adds folders to a files list.

  • volumes: the paths of mounted volumes, without hidden ones.

  • applications: names of the running apps that show in the Dock.

  • windows: names of the front app's windows.

  • finderSelection: paths of the items selected in Finder, one item each.

  • variables: the names of the variables set in this run. Names starting with __ and the error variables are left out.

  • dictionaries: the names of the variables that hold records.

  • dictionaryKeys: the keys of a record, sorted. Given a list instead, it gives positions 1, 2, 3.

  • json: the items of a JSON list, or the keys of a JSON object.

  • substrings: the pieces of text that match regex. With no regex, one item per character.

  • range: numbers from from to to counting by step (default 1, may be negative).

  • pastClipboards: the text of recent clipboard history entries, newest first, up to limit (default 20).

  • foundImages: always an empty list today.

  • lines: the lines of text.

For dictionaryKeys and json, the source is name (a variable) or else value. Text is parsed as JSON. path then walks into it, as in items[0].name.

Parameters #

ParameterTypeRequiredValuesWhat it does
intotextYesThe variable that receives the list. Required.
kindone ofYesfiles folders applications windows volumes finderSelection variables dictionaries dictionaryKeys json substrings range pastClipboards foundImages linesOne of the kinds above. Required.
fromany value, to, step: The numbers for range. from and to are required; step cannot be 0.
hiddenyes / noInclude hidden items. Default off.
includeFoldersyes / noWith files, list folders too. Default off.
limitnumberHow many entries pastClipboards returns. Default 20.
nametextThe variable to read, for dictionaryKeys and json.
pathtextThe folder, for files and folders. Required there. Also the walk path for dictionaryKeys and json.
recursiveyes / noGo into subfolders. Default off.
regextextThe pattern for substrings.
stepany value
texttextThe text for substrings and lines.
toany value
valueany valueA literal source for dictionaryKeys and json, used when name is empty.

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

List three lines and show the count
Do
  1. List lines into colors
  2. Repeat for each color in {{colors}}
  3. Show “{{ i }}: {{ color }}” for 1 s
Open in Switchboard to try it
Count up by twos
Do
  1. List range into evens
  2. Show “{{ length(evens) }} numbers” for 2 s
Open in Switchboard to try it

Gotchas #

  • files and folders fail with "no folder at ..." when the path is not a folder, and with "collect files needs a path" when you give none.

  • applications, windows and finderSelection use AppleScript through System Events or Finder, so they need Automation permission for those.

  • range fails if it would be longer than 10,000 items. A bad regex fails with "isn't a valid regular expression".

  • pastClipboards needs the Switchboard Engine and its clipboard history; without it the step fails.

  • A kind that is not on the list fails and names all the valid ones.

  • In a dry run the step is simulated unless it is marked dryRunSafe.

See also #

Repeat, Set variable, Run shell script