The New Keyboard Maestro "For Each" Action

March 20, 2012 by Gabe | [mmd] |

The new 5.1 update available today from Stairways software provides a new action. It's an action I have desperately wanted. The "For Each" action looks mild-mannered enough but it provides an opportunity to drop a lot of custom scripting from my macros.

The action can grab the current Finder selections and pass each one as a file path to the subsequent actions for processing. Previously, I had to do this with a AppleScript, so this is going to save a bit of time.

In addition to processing file selections, the action can also process every file in a predefined location, every line a in a specified file or even every line of the clipboard. Stairways added a lot of depth to the function.

For Each Action


Here are some basic examples to demonstrate the action. They are not all that useful, but I'm just trying to get some ideas down. Hopefully these are self explanatory.

Concatenate Files Example

Here's an example to demonstrate what the new action is capable of. The action works on a Finder selection of multiple text files. The result of the macro is a text window with the concatenated file contents.


Concatenate Files


Convert to List Macro

This macro demonstrates the clipboard text processing. After selecting several lines, this macro will convert them into a Markdown list format.


Convert to List Macro


Encode URL

In addition to the new Flow Control action, Keyboard Maestro added a new text filter for making URL safe strings. In actions that support text filters (like the Filter Clipboard action) the function "Percent Encode for URL" will make a string like this:

This is ain't no good for a [url]

Into a string like this


Note: Looks like the action skipped the single quote. Looks like a bug to me.

The three action macro looks like this:


Encode URL


These are very basic macros. I've done a good amount of this work with AppleScripts, Shell scripts and Python in the past. But this is an example of why I use Keyboard Maestro. It lets me do other things instead of writing AppleScript, Shell Scripts and Python.