Reference Hazel's file path

Does anyone know how to reference Hazel’s matched file paths in a KM macro?

Here's one way of going about it:

set theFile to POSIX path of theFile

tell application "Keyboard Maestro Engine"
	setvariable "Hazel Matches" to (getvariable "Hazel Matches") & theFile & linefeed
end tell

This will create or append to a KM variable called "Hazel Matches" (unless you change it to something else, of course) and add the path of every matching file to it, which can then be processed with For Each like this:

If you choose to go this route, I'd recommend adding a final action to your macro to clear the Hazel Matches variable for the next time the rule is matched:

1 Like

Wow! Thanks! I had no idea it would be so straightforward.

This seems like a very powerful set up to me, and I’m surprised I haven’t seen more of this idea in the forums. KM isn’t built (to my understanding) to dynamically watch a whole file system full of folder paths, but Hazel is…and every one of those file paths can be passed to KM for much more processing than Hazel can provide. Awesome!

You did answer the specific question as posted. Thank you for that. However, if I understand the example correctly, it sets a variable but it doesn’t trigger the macro. To accomplish that, I would think the simplest approach would be to put in the script, a command to execute the macro by name or UID.

If that’s the way (I presume it is.), wouldn’t there be concern over the variable changing as the macro is running?

I would have imagined a “per match” variable and triggered macro combination, where the macro has a semaphore lock so that the file paths would cue up for subsequent executions of the macro.

But wouldn’t that would mean multiple instances of the variable would need to exist such that each instance of the variable represents a cued up file path waiting to run in the subsequent macro executions.

Am I confusing anybody else? Ugh. Either this is not as straightforward as it first appeared, or I’m making this harder than it is. :confused:

“From wow to :confused: in a few sentences! :head_bandage:

You're welcome! Glad I could help. I do know what you're talking about, and fortunately, my testing shows that KM is smart enough to work with only a single variable value at a time. To have Hazel run a KM macro automatically on every matched file as it is matched, you can just use this AppleScript (substituting the name of the macro I used for my testing with the actual one you want to use, of course):

tell application "Keyboard Maestro Engine"
	do script "Hazel Match" with parameter theFile
end tell

This will pass the matched file's path to the macro in the %TriggerValue% token, which can either be used as-is to perform actions on the matched file, or set to a variable to enable more possibilities. In this very simple test macro I made:

Hazel Match.kmmacros (2.1 KB)

I confirmed that each matched file's path was displayed successfully, with no duplicates, despite all using the same Test variable. Hopefully this will be enough to get you started with a working intertwined Hazel/KM setup, but if you run into any other issues or have any other questions, feel free to report back! (and if you're successful with whatever you're trying to accomplish by using both Hazel and KM, I hope you'll report back with that too, since as you said, this could be of interest to a significant number of users once they know it's an option).

1 Like

Thanks again for your response!

From what I’ve read, any (or most) concern about variables changing might be (at least partially) relieved by using Instance veriables, which are specific to a particular execution of a macro.

Here is what the KM wiki says about instance variables… (Wiki: Variables)

Instance Variables (v8+):
• Restricted to a specific execution sequence.
• Available to Macro where it was created, AND Sub-Macros in that Macro, but for a given execution instance.
• So these Variables are private to each execution of the same Macro, even when running simultaneously.
• Available to Prompt For User Input
• As of v8.0.3, is available to scripts (when used in an Execute Script Action), HTML Prompt

Off to do some testing! :slight_smile:

1 Like