[SOLVED] Shell "find" multiple folders not working with KMVAR

I tried a few different options, but can't seem to understand what the issue is.
In the macro below I have the green actions that work and the red that don't.
Any idea what's happening?

shell find multiple folders.kmmacros (47 KB)

Here's one of the examples that doesn't work:
image

The issue is almost assuredly related to quoting strings in shell script actions. In particular, this bit:

So variables that are meant to be a single parameter should be surrounded by double quotes in the line, but not contain quotes in the variable, and generally you do not want to pass multiple parameters within a single parameter. So instead, perhaps set one variable to the setting name, and one variable to the type and one variable to the value, and then use a command like this:

defaults write "/Users/me/Library/Application Support/Witch/Settings" "$KMVAR_Setting" -"$KMVAR_Type" "$KMVAR_Value"

My semi-educated guess is that you're running into the same issue: Your non-working examples are all trying to combine two paths into one variable, then pass that variable to the shell. But each path needs to be delimited with quotes, so even when you surround each one with a set, that won't do the trick. You might try using double double-quotes in the variable around each path, but I think that would fail as well.

I believe what you'll have to do is create one variable for each path, and then reference each variable in the find command.

-rob.

1 Like

Thank you, Rob.
That is all super complex for me, as I have close to zero knowledge of how these things work. I pretty much just copy and paste, test the script, and that's all I am able to do at the moment.

I guess that's the only solution. My macro actually needs like 12 paths and it was a pain to add them all and then expand the tilde, but it's working now.

Appreciate your help, as usual! :raised_hands:

This bugged me enough to search for a way around it, and I found one...but I know there's an easier way that involves quoting, but I couldn't figure it out.

I first tried building a variable that created the entire string for Terminal, but that failed. What finally worked is this...

Download Macro(s): _The Path Creator.kmmacros (6.0 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 13.6.3
  • Keyboard Maestro v10.2

This macro starts with a list of folders to search (the green action), which makes it easier to update the macro as your needs change: Just add/remove from the list, and that's that. That's the only part of the macro you have to edit; the rest is automatic.

A bash script file is then built, starting with the header and find command, then each path is added (in quotes), then the find command's parameters. This is then saved to a file, set to executable, and run (set to show results in a window, but you can change that). Finally, the file is deleted.

I've included an "are you sure?" stopping point, so you can see what's built, but if things work as you like, you can just delete that, and the macro will just run.

This is definitely a Rube Goldbergian solution :), but it seems to work.

-rob.

Welcome to my world... we just can't let it go, right...? :wink:

You mean the whole script? Because that's one thing I considered as well. So the script would literally be just $KMVAR_Local__script. Similar to what we do with the HMTL form because of the variables.

Thanks for sharing the macro.
I created another version with fewer actions. It's more prone to mistakes if we don't pay attention to the paths, but it depends if we want more actions and fewer points of failure or the other way around

_The Path Creator (tiago).kmmacros (4.9 KB)

When it does the job, that's what matters :slight_smile:

Yep, that's good enhancement—it'll be faster, at only the expense of you having to type the quote signs around your paths :).

-rob.

Exactly. Yours is faster in terms of workflow. We just need to copy and paste, which is good.

Ok I'm still wondering why this doesn't work?
For example below I'm not using any quotes or variables:

find /Users/dannywyatt/My\ Files/Tests/Keyboard\ Maestro\ Tests/_Template\ with\ multiple\ files\ and\ folder/Folder\ 1 /Users/dannywyatt/My\ Files/Tests/Keyboard\ Maestro\ Tests/_Template\ with\ multiple\ files\ and\ folder/Folder\ 2 -type f -mmin -30

When we escape the spaces with the backslash, isn't it supposed to work?

It should, but I don't know if the backslashes survive in the shell script command. (I've had some much trouble with paths and quoting in the past that I now routinely just deal with them one by one—though the write-a-script solution here is something I'll probably use going forward.)

-rob.

I tried the exact same script with Terminal and it worked... it just doesn't work in KM

Never mind... I had another group of actions before that one, still active.
So this is weird, because that link you shared seem to address the issue with the variables having quotes, right?
If we replace the paths with paths that use backslashes, shouldn't it work? Like this:
image

or like this, where the variable in the script has no quotes at all (I believe we only need quotes when there are spaces that are part of the path, right?):
image

I mean, your solution works and yes, it will be my go to when I need it, but it's really weird how this works (or in this case, doesn't work)

At this point, someone more versed in shell script + variables + quoting will have to chime in :). I really don't know why those wouldn't work.

-rob.

1 Like