Make Execute Shell Script run more times inside a For Each loop

I often use Keyboard Maestro as a trigger for shell commands.
As is the case with the macro below.
Some of the images I have used this macro on is large files 10-40 MB. And it takes some time for each image. Last time I had about 500 images like this.

Is there a way to make the Execute Shell Script trigger multiple times, so I get more than one convert process at the time?

Trim image with ImageMagick Macro (v9.2)

Trim image with ImageMagick.kmmacros (2.4 KB)

Hey Jimmy,

I don't know if you'll ultimately get much efficiency out of that due to concurrent disk I/O and processing, but you can try.

A couple of ways.

Take your current macro – select a file (or files) and run – repeat.

I don't see anything that would prevent you from running your original macro more than once at a time.

Another way would be to farm each execute-shell-action out to an asynchronous sub-macro.

But if you did this too many times at once you'd very likely choke your system.


Thanks for the ideas. I will try to see what I can come up with.
Maybe I will split the list of files in chunks of 5 and make it run 5 processes at a time and then run the next batch.

1 Like

Instead of “Display Results in a window” choose “asynchronously” as shown here:

and the script will run, but Keyboard Maestro will not wait for it to finish. So you can run it as many times as you want.

1 Like

This works. But as Chris points out I need to be carefull with choosing to many files for this because it would tax my system to much.
But it is faster with 10-20 images than doing them sequential.

At this point you may have hit the limits of what you can do to manage this yourself.

If you really want to optimize this process further without over-taxing your system, then you might want/need to check out GNU ‘Parallel' which will let you run commands in parallel (hence the name).

That being said, while I’m aware of the tool, I have not used it myself, although it is on my list of things to learn more about.

Why not loop through the files in the folder in with shell command? I’ve seen a tutorial before with imagemagic that loops through files.

If I choose more than one file it does loop over the files. But only one file is processed at the time. So @tjluoma solution makes it process multiple files at once.

No. That was not what I meant.

We do something like this:

convert '*.jpg[500x>]' -quality 80 -set filename:base "%[basename]" "500/%[filename:base].jpg"

It will get all .jpg files that are wider than 500 pixels and do something conversions and then save them in a folder named 500, with the same base name, and with the file extension .jpg.

So, basically, you don't need the KM loop action to do it.

I found the tutorial I watched before:

Hi Martin

Some of the things I use Keyboard Maestro for is to not need to go to the terminal.
So this macro I posted will just take the selected files and run the ImageMagick command on all of them sequential. And so would the command in like yours if I run it in the terminal.
My goal was to find a way to process the multiple files at the same time.
And that works with @tjluoma’s suggestion.

We don't have to do it in terminal, because Keyboard Maestro can run shell scripts, as you have already been doing.

If you want to do conversion on limited selection, then looping through selection in Finder with Keyboard Maestro is probably a better choice. The sample code I got from the tutorial and posted above, however, can work on the ones whose width is larger than 500pixels, which may not be easily spotted out. For these kind of work , Keyboard Maestro can still do it, but a script like above looks much simpler to me.
I personally rarely use ImageMagick because I don't have much need of conversion. I'm not familiar with shell script either. I just put it here in case it might be useful.

1 Like