Trigger macro based on specific text in filenames?

Hi, I'm trying to get KM to run certain macros based on if one of six unique words/abbreviations are in the filenames of any file in a specific folder on my desktop (the folder is called 'INPUT'). I'm a Hazel user transitioning to KM and find it amazing but I'm stumbling on this issue so this might be obvious & I just don't see it.

Here's my situation:

  • I have a folder on my desktop called 'INPUT'

  • .jpg files are dropped into this 'INPUT' folder with pre-existing filenames always formatted like this: 'A1234567 - _Rescued.jpg'
    (the number 'A1234567' will always be different for each new file coming in)

  • The text at the end of the filename will always be one of these six different words/abbreviations:

  1. _Rescued
  2. _Adopted
  3. _OTB
  4. _PTS
  5. _RTO

Depending on which of those words/abbreviations are at the end of filename I'm trying to get KM to trigger one of six slightly different Macros I've set up for each. I just can't figure out how to get KM to 'trigger' the specific macro I've associated with each of those words at the end of the incoming filenames. In case it helps each of the incoming files includes a tag based on one of the six words too.

Any advice would be appreciated!

Hi Chris,

I'm not sure that you necessarily need to transition from Hazel to KM; I use both myself. That said, once you know that the TriggerValue token contains the full file path of a file "captured" via KM's Folder trigger, this isn't too hard to pull off thanks to the Switch or Case action. Here's an example macro to get you started:

Example Macro.kmmacros (8.5 KB)


Thank you SOO much! This is perfect and will come in handy in so many ways for me in the future! Much appreciated.

1 Like

Hey Gabe,

Hazel is down till updated for Catalina. Time to experiment!

To use this Switch/Case trigger to file docs moved to the desktop what variable do I use in the Move/Rename Action? I tried %FinderSelection% but I guess the Switch/Case action isn't actually selecting anything and/or %FinderSelection% is the wrong variable. The readings in the wiki for Switch/Case and TriggerValue are above my current pay grade :wink: .

The next issue looming will be when the file title won't work and I'll need KM to parse the file (typically an email of PDF) to get a value to act on.

Hazel's Contents/Content Match has be good about reading most anything I throw at it and I'm guessing KM will need more from me to read variously formatted docs.


Found this:

Aug 20


Perhaps the Reveal File action has changed behavior during the last 4 years.

It is not the Reveal files that has changed, it is the token %TriggerValue% , which used to return only the file name. Now, as you have learned, it returns the full POSIX path.

So maybe the variable in the Move action needs to include the name AND path?

I don't know enough about your workflow to say for sure, but I'm not sure you need a Switch/Case Action. The below macro shows you how to use a Folder trigger with a Move File Action. Of course you will need to just use this as an example or mod to meet you specific needs.

Hi @JMichaelTX,

Here's the macro and damn, it works! I'm still at a level of understanding where I'm genuinely surprised when it works which I know seems silly from a perspective given by a thorough understanding of the elements and their interactions.

I understand and see that the Set Variable action creates a Variable and sets it to the TriggerValue token in step one and that that Variable is later picked up by the Move action as a Variable imbedded within the Switch action:

As said above:

So using a TriggerValue token within an Action within the Folder Trigger action that is monitoring the desktop folder in this case is having KM continuous scanning the Desktop folder for newly added files given the setting "adds an item" and the TriggerValue token is reading and storing the TEXT STRING defined in the first part of the Switch action AND the FILE PATH where that string occurs.

QUESTIONS: Does this scanning only scan file names or does it scan the file contents as well? If it does not scan the file contents, are there additional actions that can be added to have this macro do that as well so the Move action can move a file based on the file name OR file contents?

Then the Switch action set to text makes the value of the TriggerValue token scan for text rather than one of the other possible values.

So the Set Variable action in step one along with the setting in the first part of the Switch action all are for the benefit of the Move action embedded in the Switch action to tell the Move action what to move and we're using a Variable because what is being targeted is not a fixed string.

BTW, I'm being verbose in this writing to firm up and expose my understand for corrections and omissions. I appreciate any feedback.

Thank you for this.