Building an array variable from Finder items matching criteria

Simplified Summary
Hoping to loop through a number of items, adding each one to an "array", then target that array later in the Macro to select an item from the list.

What I'm attempting to achieve is to populate a KM Prompt With List action with the (folder) names inside a (static / specified) directory.

Here's what I have so far:

  • For Each loops through the items in specified directory
  • Logic to filter for only Directory type
  • Get and set Var with the name of the Directory from each item's path
  • A counter Var for incrementing inside the loop
  • A Prompt With List from Var

What I can't figure out is how to add each item, or %Variable%item[%Variable%count%]%, to a Var in order to target with the Prompt With List, outside the loop. Right now it's returning only the final item (FolderName) from the loop.

For clarification, the folders inside the directory change often, otherwise I would define a static text list of the folder names.

Is this best suited for a Dictionary instead of a Variable?
Do I need to leverage AppleScript instead?

Can anyone point me in the right direction? :v:


KM does not support true arrays, only pseudo-arrays using text in a KM Variable (which are ALL text), separated by commas.
IAC, for your use case, I think all you need to do is to add Directory name to a simple KM Variable using a simple Set Variable to Text action, with each Directory Name on a separate line, something like this:

For Each Action
Set Variable "Directory_Name"
END of For Each

the to: block is this:

where, "Directory_Name" is the name obtained by the "Get and set Var with the name of the Directory from each item's path"
%Return% will add a CR character at the end, so that each Directory Name is on a separate line.

You should initialize the "Directory_List" variable to nothing (empty string) at the top before you start your For Each loop.
You will then use the "Directory_List" variable in the Prompt with List action.


If this does not work for you, please post the Macro you have so we can see what you are trying to do.

1 Like

Awesome - thanks for the clear instruction and quick reply! Works like a charm! I'll apply some abstractions then upload the Macro sometime soon.

Thanks again, @JMichaelTX


1 Like