Distribute File name to different files within the same folder (in combination with Hazel)

Hi all!

I've created a specific folder watched by Hazel. Therein there will be three (4) files. One, a .zip folder, contains a *.pdf. The other two file are a *.mobi & an *.epub. Once unpacked with Hazel the now *.pdf file is the one with the correct [full] file name.
So what I'm trying to do with KM is get the full name from the *.pdf-file and rename the *.mobi & *.epub with the same name as given for the *.pdf. Once the three files all bare the full name I'll be moving them to their own folders using Hazel.
I'm a complete newby to KM. I did try some stuff using FileName or VarName and I think I got it working so far as to add the correct file name to the (Named)Clipboard but I just can't find out how to paste that name to the two files in questions. I assume that once I've created this step with KM I can easily (?) have this macro be used by Hazel.

Any input would be most appreciated!

It sounds like you would be better served by KM's For Each action in conjunction with its file manipulation action than by trying to copy and paste the PDF file's name. Try plugging the path to the watched folder into the blue "Set Variable" action at the top of this example macro and see if this doesn't get you most of the way there:

Example Macro.kmmacros (5.4 KB)


Hi @gglick, this is great. A kind of cheat-sheet on how to use these Actions in combination.


Hey Gabe!

Thanks so much! I did try your macro both first modified for my folder then exactly as you've created it. Modified for my folder nothing at all happened (that I'm aware of) trying the "Try" or "Run" option and when using your exacta copy, after creating the Desktop test folder, I get the "Files Renamed" message but without the files actually having been renamed.
Is there something I might have not done or forgotten to do/change? Could Big Sur running on M1 be causing this?

Hi Zabobon!

Just wondering. Did you by any chance try it out on your system and have it work?

You're welcome. For comparison, here's my test folder and files before and after:



When running the macro, you should always use "Run," as "Try" only executes the currently selected action. As far as I know, there's nothing about the M1 that should prevent this macro from working. You may need to give the KM Engine full disk access in System Preferences > Security & Privacy > Privacy > Full Disk Access if you haven't already, though I would assume you would have gotten an error message to that effect if that was the reason it failed.

Did you enable both the macro and the macro group before running it? I assume you must have, as you couldn't have gotten the "Files Renamed" notification otherwise, but downloaded macros are imported as disabled by default, so you may want to double check that isn't a factor.

Beyond that, I can only suggest posting a screenshot of the files you're testing with, just in case there's something about their names that the macro doesn't account for.

Thanks so much for your time. I did now also give the KM app full disk access (even if it's stated that giving Accessibility for Keyboard Maestro and Keyboard Maestro Engine is good enough) but I'm not sure how I'd give specific full disk access to the KM engine as well.
Nothing changed tho so I'm posting a screen shot. Maybe the brackets have an influence? As I thought I read all variables will be read as "text" if not specified otherwise so that they & the number shouldn't matter, no?

OMG I'm so sorry! I named my desktop folder test instead of temp so it actually makes sense that nothing happens within my test folder....
So your macro is doing exactly what it should and is working perfectly.

Now I'll be trying to add it to Hazel.

Thanks so much for now!

1 Like

Just wanted to add and let you know that combining it with Hazel worked like a charm. One of two reasons might have kept it from working at first. Either my directory was not set correctly ~/Downloads Take Control Books (Nicht löschen!) instead of ~/Downloads/Downloads Take Control Books (Nicht löschen!) or as you had mentioned the macro group hadn't been enabled beforehand.
Anyway I'm a very happy camper now, so thanks again!

1 Like

Going very slightly off-topic (but might be of benefit to people reading this thread).

I always found/find the For Each Action quite confusing as it uses a user-defined variable at the start but in examples it looks like this variable has to have a certain name (when in fact it can be called anything, as long as that name follows through in the following Actions).


To make user-defined Variables easier to spot I really like JMichaelTX's naming convention of having a prefix followed by two underscores when creating Variables. So, in the above example I would name the Variable LocalFile as LOCAL__File. It makes the Variables easy to spot as Variables rather than Tokens and much easier/safer to automate a Find and Rename of all the Variables in a Macro. The two underscores also have the effect that if the Variable is used in a User Prompt the prefix will be hidden.

Apologies again for going off-topic in a thread that is already solved but this one tip has been incredibly useful to me in building Macros and in understanding what the different parts of a Keyboard Maestro Action are actually doing.

Thanks Zabobon! I'm always eager to learn and having a mindset for naming conventions right from the start is a great idea. Having read your and the linked post a question does arise. Shouldn't in your example the LocalFile variable read something like MACRONAMEABRIVIATION__LocalFile instead of LOCAL__File? That way in general keeping more information for a possible prompt when loosing the prefix. Please DO correct me or let me know what I'm not considering.

Using "Local" at the start of a Variable’s Name is good practice if you don't need to have the Variable's value exist beyond one run of the Macro. Any Variable which starts with "Local" will be automatically cleared out after the Macro has run (which is why @gglick called the Variable "LocalFile" rather than just "File").

By separating "Local" from "File" by two underscores it means that if that Variable was used in a Keyboard Maestro "Prompt" the prompt would just show the word "File". And using capital letters for the prefix simply helps to see at a glance that this is a user-defined Variable.

In other words, "Local" or "LOCAL" is a special Keyboard Maestro keyword that when used at the start of a Variable's Name tells Keyboard Maestro to empty the Variable after the Macro has run and as such I would want to see at a glance which of my Variables are "Local" and which would be retained for the use of other Macros.

So, I might call a Variable that will be used by other Macros "WRK__file" where (for me) the "WRK" means something and know that the value this Variable contains will be available to other Macros. And in the same Macro, give another Variable the name "LOCAL__File" where I don't need the Variable's value outside of that Macro's single run.

1 Like

Just to avoid future misunderstandings, the KM wiki about variables states that variables that have names starting with Local behave in the way you describe. The same applies to Instance variables.

In other words, it is not enough to just have the word "local" somewhere in the variable's name.

Get the full definitions here: manual:Variables [Keyboard Maestro Wiki]


1 Like

Of course you are right @tiffle - yes, “Local” needs to be at the start of the Variable name if it is going to have any effect. I have amended the wording in my post! :grinning:

BTW very strange that @JMichaelTX is no longer an active tag. Very sad.

1 Like

Wauw Zabonbon, thanks for the lesson; seriously! Great and comprehensive explanation for a Newbie like me.


Thanks for clarifying Tiffle. I just had a look at the variables manual and the "local/instance variable thread"; now my head is spinning...
Well looks like I'll need to put some time into all of this but it sure seems worthwhile and I'm amazed how helpful all within this thread were.