Pass Multiple Variables to a Script

Hi!

I’ve just started with Keyboard Maestro a week ago but I absolutely love it. Tons of things to learn. I am currently hitting a wall creating a macro that multiple variables need to be passed.

The content of the macro

I want to use ImageMagick command tool to create a collage from selected images.

The command used here is

montage image1.jpg image2.jpg -geometry 800X800+25+25 -density 300 -quality 100 converted-image.jpg  

Ideally I want to be able to handle not only two images, but any amount of images selected, to pick every image’s path and put them between “montage” and “-geometry”

What I currently have

minipost%20image%20montage-pty

I searched both wiki and google, but could not find the method to pass multiple variables.

When I tried to run shell script with plain text, it seems like KM was not picking up the path. So I thought of store every path into the clipboard and paste them in the reverse order in order to pass the variables. I currently set the macro to paste last two paths, but have no idea how this would work for any amount of images.

I must’ve done something wrong as the script would not work. The macro always got canceled.

To test if the variables are correct, I added a text box to display them.

I paste the text into iTerm and it worked.

I realize I probably cannot use user input as the final file name, but it did not make any difference when I used a pre-defined name.

I appreciate any feedback or help :sparkling_heart:

Maybe a stupid question, but if the montage binary is in /usr/local/bin, why not just call it as:

/usr/local/bin/montage

instead of:

montage

And here is a simpler way to get a space delimited list from the finder's selection:

image

Maybe this will help. Where I used text input, you can use KM Variable tokens, like:
%Variable%image1%

Hey Scott,

Because it's verbose, harder to read, and more prone to error – especially when you call multiple commands in the same script.

It's a better practice to set a $PATH environment variable in Keyboard Maestro (ENV_PATH) for shell scripts, OR to export the path once in a given script as @ying did in his example macro.

-Chris

Hey @ying,

The basic method of embedding Keyboard Maestro variables in shell scripts is as follows:

Passing Keyboard Maestro Variables to a Shell Script Action v1.00.kmmacros (5.5 KB)

You can get fancy with running a shell script action with input from, but this is the simplest method.

It is however a pain in the neck to debug.

Building from the above a more rational method is to construct the shell command so it can be displayed and copied for debugging, OR executed.

Composing a Shell Script to Run from Keyboard Maestro v1.00.kmmacros (6.7 KB)

By variously enabling/disabling the Execute Shell Script and Display Text in a Window actions you can debug or run the script in question.

HTH.

-Chris