Hazel question for the experts

I would like to seek some help on a Hazel question. I know this might be better posted on the Hazel forums, which of course I can do, but since I frequent here and not there, I decided to through this out here first.


I have a generic inbox folder (creatively named _Inbox; the underscore for sort ordering) into which downloads, scans, and pretty much anything that needs later filing goes.

I have a rather large set of Hazel rules that detect many of the files that wind up their, use their contents to figure out what they are, rename and possible tag the files, and then ultimately move them to a target destination. For example, the contents of a downloaded electric bill allows Hazel to detect it, rename the file to the format <billing_date>_electric_bill.pdf and then move it to the Bills/Electric folder.

The moving part is what I am trying to modify for several reasons. First, what I like to do is have the file renamed and tagged, but then sit in the _Inbox for some time period before being moved in case I need to work with it shortly after the download, scan, etc. Therefore, since there is no Hazel action (that I am aware of) where you can place an intentional delay in the action rules processing, this requires a separate rule for each type of file, one that does the rename initially and a second one that has an added condition of “file added not in the last XX minutes” which then does the move.

Secondly, it does arise from time to time that I want to relocate the target folders of one or more type of file, and that means a lot of rule editing. This is even worse at times when I migrate to a new Mac, which I do more often than is reasonable.

So: my thought was that it would be ideal for me to have one rule that does all of the file moving. This rule would take the file name patterns (eg it would have the ability to recognize _electric_bill.pdf and _gas_bill.pdf) and would have a table that matched patterns to the target folder and then do the move to the target folder.

On the surface, the Hazel pattern matching capability that allows you to create a table would seem to be perfect for this purpose, with a pattern column providing the match pattern and a destination folder providing the place to move the file. I could easily maintain this data in an external file that could be easily editing with batch replacements and so forth.

My problem: as far as I have been able to determine, the table pattern matching in Hazel does not allow you to actually insert a pattern into the cells of the table, only fixed text, and therefore there is no way to do pattern matching of any sort - and since these files will pretty much always have the date in the file name which is obviously not going to be a fixed string, it does not look like I can accomplish this with the built in Hazel tables.

I was wondering if anyone knew something that I have not been able to find that could help here?

If not, it is not all that difficult for me to create a script that can do the same, but it’s a bit of a pain. To return the destination folder to Hazel (I would prefer Hazel do the file move rather than doing so in the script, although perhaps not for any good reason) the script needs to be in AppleScript as the only way for scripts to pass data back into Hazel tokens. However, doing regex or pattern matching in AppleScript is not what I would call convenient, so I will need to embed a script in another language (probably Python) to do the lookup in the file. Not the most difficult thing in the world, but the kludge level grows, and I would hate to miss a solution internal to Hazel that I am not aware of.

(BTW: I know that the AppleScript could use a shell command to grep the file that associates patterns to target folders, but that is backwards of how grep works, because the lines in the file contain the regex patterns which I am matching against the string of the filename, which is backwards. I am sure this could be down with something like awk, but it willl take 10 minutes to write the Python script, so I might was well.)

Instead of moving the file to the destination, copy it to the destination. Then add one new rule that trashes files older than some number of days.

If you want to check for files that match no rules, add a rule at the end that just tags the file. This rule won’t be executed if any earlier rule matches. Then have the trashing rule only work for files without the tag.

1 Like

What about, after renaming and tagging you moved it into a subfolder. Then you could have rules on the subfolder that included the trigger DATE ADDED is not in the LAST xxxxx.

Or something like that

1 Like

I don’t use Hazel for anything g this complex, but it sounds like you want a something like configuration file.

Can a Hazel rule read from a file? If so, you could save the locations/paths for specific rules in a plain text file (even as simple as a single line of text), and have Hazel read that file to retrieve the destination path.

Alternatively, is Hazel scriptable? If so, you could periodically run a script to change the destinations in each Hazel action. If you have a tool like Keyboard Maestro, you could set this script to run each day before you wake up, and/or at login; you could also remember to run it when you change the destination for a particular kind of file.

None of these is simple, but then again, you’re trying to do something pretty complicated. How often do the file locations change?

I only answered part one of your question. For part two, relocating target folders, why not use aliases? The rules point to the alias, and the alias points to the desired folder. No rule editing at all. You just change the aliases in Finder.


Thanks for all the input.

@tomalmy Using an alias for the target is not a bad idea at all. I will look into that. Part of the issue is that different files go into different folders, so one option is a tree of aliases. Another is perhaps the sort into subfolder action. Have to play with this a bit.

I am reluctant to do a copy to destination because I don’t want the duplicate files, even for a short time interval. That also isn’t solving my biggest “want” which is to have the file locations derive from a table rather that being hard coded into each rule. Effectively I am trying to separate the moving logic from the identifying and renaming logic.

Generating a utility to parse a text file which basically has a table of regexes and target locations and match the file name with the proper regex and return the location is simple enough. Took about 15 lines of Swift code. It has to be wrapped in AppleScript to return a value to Hazel, as far as I my understanding extends. I might just go with that for a bit.