[SOLVED] Find path using RegEx Macro

I would like to use an IF THEN action where the path could match different files.
Something like this:
image

Where (Bad dreams).*(logic)$ is my RegEx.
So it would match these two:
image

I found this other topic that seems to be similar, but I don't know how to then use the script in the IF THEN action:

Here's my macro with the script ready to go.
Find path using RegEx.kmmacros (22 KB)

I'm not sure I completely understand the request, but here's what I think you're asking for...

If, in a specified directory, at least one file exists that matches a regex pattern, then [do some stuff]

Is that right? If so, then why not move your "find" logic test up above the If/Then, and set it to store its results in a variable. You can then check if that variable is empty (no matches) or not empty (matches), and do you if/then only if not empty.

(Or set the If/Then condition to be that shell script with the rule being results not empty. This saves the variable step.)

Or am I misunderstanding the nature of the task?

-rob.

1 Like

Yes, that's what I want to achieve.
For example if I have 100 files in a folder and some follow a certain pattern, but 1 or more words are changed
File 001 A
File 002 A
File 003 A

I want to be able to target those only and ignore the other 97.

So your first option would be this:

And the second one would be this:

Right?

Right. The advantage of the variable method is that if there are matches, they'll be in that variable, and you could then just iterate through that to do what you need with "lines in a collection > variable."

-rob.

1 Like

I tried them both and don't seem to work, because I think there's an issue with the script

find \
~/'My Files/Tests/Keyboard Maestro Tests/RegEx in paths' \
-type f \
-depth 1 \ 
-iname "(Bad dreams).*(logic\.rtf)$"

I get this error when I run the macro where I have the script result to a variable:

Action 15402189 failed: Execute a Shell Script failed with script error: find: : unknown primary or operator
text-script: line 5: -iname: command not found
2023-11-22 15:34:32 Execute a Shell Script failed with script error: find: : unknown primary or operator
text-script: line 5: -iname: command not found. Macro “Find path using RegEx 1” cancelled (while executing Execute Shell Script).

Do you know what is wrong here?

Also, I think the script is looking at the full path, including the file's extension, right?
I added \.rtf just in case, but either way, they don't work.

Can you post the full macro?

-rob.

_Tests Danny Wyatt Macros.kmmacros (46.1 KB)

Both of them

Ah, wait, I see the issue.

-iname "(Bad dreams).*(logic\.rtf)$"

You can't use regular regex structures in iname. macOS uses a simplified structure:

-iname "Bad dreams*logic.rtf"

That should work (at least, it does here in testing). If you need more complex pattern matching, you'll have to pipe the output of find to grep or awk or something.

-rob.

I changed it and still not working:

Action 15402274 failed: Execute a Shell Script failed with script error: find: : unknown primary or operator
text-script: line 5: -iname: command not found
2023-11-22 18:51:19 Execute a Shell Script failed with script error: find: : unknown primary or operator
text-script: line 5: -iname: command not found. Macro “Find path using RegEx 1” cancelled (while executing Execute Shell Script).

But if things need to be simpler, how would the script know for example that a certain word is supposed to be at the end? Or if the . is an actual dot and not the RegEx "match any character"?

Ok I was able to make it work:

image

Of course this solution doesn't necessarily answer my initial question, which was more focused on the condition looking for a file in a path, but for now this seems to do the same thing

Yeah, I'm not sure that's possible, though I know it's not possible with my skill set :). I was just trying to find another way to get the result you were looking for.

-rob.

1 Like

Well, mine is not even close to being "ok"... haha
And I've been learning more things than I expected such as RegEx, AppleScript.
It's just a lot, especially because I'm not a developer or anything. These are not things I will use on a regular basis. I'm just creating a workflow for my music creation and promotion process.

I appreciate your time and help though :slight_smile:
At least these roadblocks help me think of different solutions, which is good.

Nor am I, which is part of why I like KM so much—I never could've written something like the MacroBackerUpper thing in a 'real' programming language. And I got to the point where I could write something like that much as you are, fumbling through my earlier macros, learning bits of regex, AppleScript, shell, and sqlite database stuff (which is incredibly useful).

It's not a fast or easy process, but it's rewarding when you make something that works just like your mind envisioned it!

-rob.

2 Likes

100%

It's a very tiring and sometimes very frustrating, especially when it's something super obvious to some people that get there in like 2 seconds and you spent 2 hours trying to figure it out... haha