Hazel rule to run any time files are added to a folder (or sub-folder)

I’m trying to set up a Hazel rule that will run a bit of AppleScript any time a new file is added to a specified folder (or any of its subfolders). I’m brand new to Hazel, and I’m struggling a bit to get this done. I’d greatly appreciate some guidance on how to get this working.

Just wanted to provide a bit more details in case it is relevant. The folder I’m trying to monitor is my Calibre library. My goal is to have the AppleScript run any time a new book is added to the library. The AppleScript is set to run a Keyboard Maestro macro to sync my library to my BookFusion account.

Not at my Mac right now, but maybe copy the URL to trigger the Keyboard Maestro macro, then when you get a match in Hazel for any new file, have it open the URL.

The terminal command open followed by the URL should suffice.

However, this will run multiple times if you add multiple files, which seems like a waste. Have you considered having Keyboard Maestro check your folder periodically (it has built in scheduling), count the number of files, compare it to the previous number (you store it in a variable to reference between runs) and if different, run the sync then.

Hope that helps.

You could run it every 10 minutes or every hour for example.

I think I have the action side of things figured out. The part I’m struggling with is how to set up the Hazel rule so that it triggers any time a new ebook file is added to my Calibre library. Since the library is just a folder with subfolders for authors and books, I need Hazel to run the action whenever a new file is added to the subfolder for the book.

Okay, maybe this will help with what you want to do then.

First set up a rule like this to trigger other rules on sub folders. This is just the standard set up as described in the inbuilt Hazel help to process sub folders.

Then set up the shell script to open the macro URL in another rule as I described above - set to trigger when a file is added.

You may want to tweak the latter rule to match relevant file types or run your AppleScript instead if it is doing additional processing beyond just running a Keyboard Maestro macro; and as I noted earlier, if you move in several files to the folder structure, this Hazel rule is going to trigger several times.

Does that give you enough to do what you want?

Thanks. This is definitely helping. Would the rule you outlined above trigger off all of the files already in my Calibre library? I wasn’t sure if I needed to use the date created is after date last matched condition. I definitely don’t want it immediately running the rules hundreds of times as soon as I start it.

You could use an additional Date Added condition to ensure nothing triggers.


Don’t forget also, you can test things out on a smaller scale with rules on another folder. Create a folder for testing Hazel rules, then add a rule with whatever conditions you want to test against, and then maybe something for feedback. For example, this AppleScript just speaks the name of the matched file.


If I pause the folder and preview this rule against a particular file, I can see if there are any matches for it to process, and it will show what conditions are matched.


Here I selected a file that was added a couple of minutes prior.

I can also view check the status (blue icon to pop up the results boxed in blue). I can see the files that Hazel has already processed (boxed in red), and also, because I just added a new file while it was paused, I can see there is an unprocessed file waiting to be processed.

Hazel gives you helpful ways to check how rules will run before you try them, but I would always recommend running small scale tests on a test folder, and always having a good backup for any folder you are letting loose automated rules on where there is any chance you may not have accounted for all scenarios and could suffer data loss as a result.

1 Like