Shell Script Space Rule

Hi,

About the shell script wiki – does this rule still work?

"Keyboard Maestro sets the environment variables for the script to include all your variables, using a prefix of KMVAR_ and your variable name with spaces changed in to underscores (_). For example, your Keyboard Maestro “File Name” variable will be available as the environment variable KMVAR_File_Name ."

I find that it's not working if the KM variable has spaces:

Hey @som,

When asking for help please post complete macros that can be tested – preferably the simplest possible test-case that demonstrates your problem.

Macros contain fiddly details that cannot be conveyed through prose or images, and these details are often key to solving a given problem.

I can't test your macro without reconstructing it, and that's not an efficient use of my time.

This works perfectly well on my macOS 10.14.6 Mojave system with Keyboard Maestro 10.2:

KM Variable With Spaces to Execute Shell Script Action v1.00.kmmacros (5.5 KB)

Macro Image

Take Care,
Chris

(Keyboard Maestro Moderator)

Hi ccstone,

Thanks for your reply.

It works if I just replace the $KMVAR_image_folder to the exact folder path.

Here is my macro:

Download: rename image of folder.kmmacros (3.4 KB)

Macro-Image

image

System Specifications
  • macOS Ventura13.2
  • Keyboard Maestro 10.2)

That's quite a bit different!

It's failing because you are passing in a ~ path from your variable, and the ~ prefix doesn't expand to your home folder path when it is quoted. But you can use a "Filter" action set to "Expand Tilde" to get a full path to pass to the script. Try this example, first with the second action disabled, then with it enabled, to see what I mean:

tilde expansion.kmmacros (2.3 KB)

Image

Nice improvement on the shell script. Here's what I'd got to before you posted (note the use of a Local variable in KM, something you should consider unless you want the value to persist or be available to other macros):

#!/bin/bash
cd "$KMVAR_Local__image_folder"
i=1
for file in *
do
	mv "$file" "$i.${file##*.}"
	((i=i+1))
done
1 Like