Help With Image Manipulation and Variables

Hi guys

so i tried this macro below to use CLI image magic to process an image

I'm trying to whiten an image and the command needs a input image and an output image specified. I want to add a character (like '2') after the filename and before the extension. I'm sure its simple and I'm an idiot.

Can someone guide me on this?



Hey Z,

In such cases please provide the macro file (or actions file) associated with the image, so people don't have to reinvent the wheel to help you.

Please also provide a working sample of the shell script you're using that you've tested in the Terminal (if you have one).


  1. Change the “File” variable name in the for-each action to filePathStr, so something more evocative of what it really contains.

  2. You're monkeying around with file-name and file-extension, when you've failed to tell your shell script where to look for files. You need the script to cd change directory to the container directory of the file(s) to be processed (OR you need to provide full paths).

  3. Use a Keyboard Maestro environment variable (ENV_PATH) to set the path for shell scripts run from KM ($PATH).

Mine looks like this:


Running this FROM Keyboard Maestro will get you something close to what you need.

Execute a Shell Script.kmactions (661 B)

Shell Script for Above Action
printf "/usr/local/bin:$PATH"
  1. It's not a good practice to compose your command line IN a Execute a Shell Script action. This practice is fraught with opportunities for failure (or disaster) and is very difficult to debug.

  2. It's a better practice to compose your shell script in a variable, so you can safely run the macro and eyeball the script before actually running it.

Compose a Shell Script Before Running It v1.00.kmmacros (7.9 KB)

  1. Be sure to get your command running correctly in the Terminal, before even beginning to work with Keyboard Maestro and variables.

If you're still stuck then holler.


1 Like

thx @ccstone, appreciate the detailed response

I will look into all the tips you gave. One thing I didn’t make clear was that I'm trying to launch the CLI on the currently selected finder files so I assume I don't need the cd step.



P.S. This is my current in progress macro :slight_smile:

Whiten image .kmmacros (3.6 KB)

Hey Z,

That requires using the path and not just the name and file extension.

For commands like this it's much safer to cd into the target directory and use the file name-extension instead of the full path. You're much less likely to make a mistake that can destroy something unintended.

ImageMagick's convert command by design cannot edit a file in-place, so the syntax you were using could never work.

To use convert like this you have to provide the source and destination file names (or paths):

cd ~/'test_directory/ImageMagick_Test_Folder/'
convert test.png -fill white -colorize 80% 'test copy.png'

On the other hand ImageMagick does provide a means to edit files in-place.

The command for this is mogrify.

So – this macro follows the intent of the one you posted:

Whiten image.ccstone v1.00.kmmacros (6.3 KB)


You can even speak with authority on ImageMagick! Can you make a short list of everything you don't know?



It wouldn't be short and would be too difficult to compile, because even I don't know everything I don't know...  :sunglasses:

By the way – I'm not that knowledgeable about ImageMagick. I've used it for this and that over the years but not that often.

I'm thinking either @mrpasini or @Tom are the likely authorities on this forum.

However – I can research the issue with Google – and read a manual.


thx @ccstone
that’s perfect



Resurrecting this thread because I'm trying to do something similar, namely converting from jpg to png.

@ccstone I read through all the comments, downloaded your macros and walked myself through them to try and understand them. I would just like to understand the Shell Script itself a little better.

In my test macro below, I am able to successfully set the variables, correct the file path for use in the Shell Script, and then run the SS successfully. I would just like to understand the three different parts of the SS for potential future use. Could you expound on what they do?

Here's what I think they do...

shCmdStr="$KMVAR_debug_shCmdStr" - sets a SS "variable" to whatever the KM variable is?
printf "$shCmdStr" - no clue what this does :sweat_smile:
bash -c "$shCmdStr" - obviously runs the command... but why the bash -c? Just to indicate which kind of shell it needs to use?

Sorry for what are no doubt very newbie questions... but you've helped me so much with AppleScript these past few months, and try as I might with Shell Scripts, there are a lot of things I just don't get still.

Any help is appreciated, and thanks as always!


Imagemagick convert image format shell script.kmmacros (3.6 KB)

Macro screenshot (click to expand/collapse)

I have this Automator workflow as a service. Doesn't use KM nor ImageMagik.

Note: Needs ImageOptim installed (free)

to JPG (327.3 KB)

Hey Chris,

This is not strictly necessary, but I don't like a bunch of variables with monster prefixes all over my shell scripts.

printf "$shCmdStr" - no clue what this does :sweat_smile:

In the Terminal:

man printf

Google is also your friend.


I suppose I wasn't entirely clear here.

Uncomment the printf line and comment-out the bash line...

To eyeball the code without running it.

bash -c "$shCmdStr" - obviously runs the command...
but why the bash -c?

man bash

Look for the -c switch.

Normally bash <string> expects the string to be a file name/path.