How can I use a filename pattern as trigger or condition?

Hi wizards,

I've spent the whole day (literally) on this seemingly simple issue. And after scouring numerous pages of this forum, I think it's better to resort to your wisdom.

I'd like to achieve the following :

  1. Copy an image file in Finder (as simple as Cmd + C)
  2. Have KM trigger a macro only if the filename starts with a certain pattern, let's say Design_
  3. The said macro should OCR the image file and copy the result in a variable
  4. Have a Youtube search open in the default browser with the variable.

All my trouble lies in step #2 : I can't figure out how to use a part of the filename to as trigger or at least as condition.

That'll allow me to check if #3 works. #4 is OK.

Any help is appreciated. Thanks in advance!

Would the 'Folder' trigger do the job? Have KM move the selected file into a folder (the trigger), check the name, if not Design_ exit macro.

Okay, here is a way to get over that step using the information you have given about what you want to do.

You have said that this will happen in the Finder and that you will press Cmd + C to copy the file to the Clipboard.

So, one way is to have a Macro that is only active when you are in the Finder (by putting it in a Finder Group) and then triggering the Macro when you press Cmd + C.

The first Action in the Macro is simply Keyboard Maestro's Copy Acton. This effectively means that Cmd + C will still do its normal task of copying the selected file to the Clipboard.

Then Keyboard Maestro makes a Variable from this copied Finder Item. As Variables can only ever be plain text, that Variable is the name of the File you have just copied, not its contents and not the image. But the image will still be in the Clipboard, ready for whatever else you want to do with it.

Then you can use an If Then Action to test if that Variable name starts with (as in your example) "Design_". If it does the Macro will branch one way and if it doesn't the Macro will just end there.

EXAMPLE Use a Finder Copied Filename as a Condition.kmmacros (7.0 KB)


This was the key! I didn't know this was the default, I was trying to have the file name copied in a named clipboard while the image was in the system one.

Now it works! And most importantly, I've learned a lot along the way.

To keep work things at work, I've duplicated the macro for a personal use: search a music video on Youtube based on a Shazam screenshot. The only thing that changes are the variable names. Here you are:

Thank you again, folks :wink:

1 Like

And what happens if you have 2 (or more) items selected in the Finder? Or the file name selected and active and do a ⌘-C?

Worth considering these things -- or, at least, knowing what will happen so you don't get taken by surprise when it does!

1 Like

Yes, it might be a good idea to build in some more conditions to the If Then Action. The Example I gave was a kind of starting point to show how these things can be made to work.

Also... although it works using ⌘C I personally would never actually have a Macro set up with that System shortcut. Assuming you know when you want to do a Shazam check it would make things a lot safer if you picked a non-system shortcut like Control+S or Option+S etc (S for Shazam) for the Macro.

The Macro would stay exactly as it is now (with Copy as the first Action, getting the file name into a Variable and the If Then the same) but would avoid the kind of surprises @Nige_S has mentioned.

A few points :

  1. I've tested with two files, and it only picks one to run the macro. This is OK for me, I'll need it for only one file from time to time.

  2. Using Cmd + C was for testing purposes, of course I've already picked another shortcut for the work macro (Control + Shift + S).

  3. Last, while testing, I tried to add "Clipboard contains image" as a second condition Ifcondition, but it was preventing the macro from running. More preciseli, it seemed to prevent the OCR part. I can't understand why.