Auto-Increment File Names in OS X with Hazel

For client artwork, I save a lot of versions of files. Enough so that I got tired of manually incrementing the file names when saving JPEGs from Photoshop and decided to see if Hazel could automate this for me. It wasn’t apparent how to do this at first, but the Hazel developer steered me in the right direction. Here’s how to do it.

First, set up a rule as follows:

Then, click in the attributes field after the “with pattern:” and set up the attributes as so:

The key attribute here is the “#” token. You can click on it to set the number pattern for as many digits as you like. I prefer two digits for visually tidy file names in the Finder.

In my case, my files are all saved as “filename-sketch.jpg” when I Save For Web from Photoshop. This saves a .jpg file in the project folder as “filename-sketch.jpg”, which matches the Hazel rule, and it transforms the filename into “filename-sketch-v01.jpg”, and then increments the numbers for each successive .jpg saved to that folder matching the filename pattern in the Hazel rule.

One minor limitation is that Hazel seems to keep its own system to do this, meaning if you delete a file that it has renamed and save a new file that matches the pattern, it will increment based on the last time the rule ran. In other words, if you delete the file Hazel renames as “filename-sketch-v02.jpg”, the next file will be renamed “filename-sketch-v03.jpg”. I can’t really see any way for Hazel to avoid this.

9 thoughts on “Auto-Increment File Names in OS X with Hazel

  1. Hi,
    Great improvement in my workflow, thanks.

    I wonder if you could help me out with another problem I have : I regularly download files from one particular site, the PDF files that are generated all have the same file name.
    The informations to give them a real name are all available in the url path (from the WhereFrom metadata).
    I tried to build some shell script to rename them but it seems I am not skilled enough to achieve anything with sed despite knowing the right regex to use.

    1. Unfortunately I’m not much of a coder. But if the URL path is the same for all files except for the actual file name, you might be able to extract the name using Hazel and tokens. Can you post some sample URLs? Otherwise, I would suggest posting your question over on the Hazel forums, usually the developer will write a script for you if he can and the ability is not within Hazel already.

      1. Well, you could set up a rule where the name matches a pattern where you tell Hazel to set tokens to match the DateFacture=20120120, and then use that token for the renaming.

        Screenshots:

        And you could probably tweak further to parse the date down even further to Year/Month/Day chunks.

      2. You still need to retrieve the url first. I tried to rename the file with the url and directly replace the colons that are not compatible with OS X namespace but Hazel then goes hamok. It looks like it enters a loop and uses all the memory and available disk space crashing the Mac. I just wrote to the team about this, I hope they have a solution for me :)

  2. It is possible to override Hazel’s number keeping system. Then each time it runs it starts afresh as opposed to picking up where it left off the last time the rules ran.

    1 – Ensure that system files are shown (Finder > View > Show System Files), you can disable this afterwards;
    2 – In the relevant folder find “.hazellock” file & delete it.

    1. Interesting. Does this interfere with other Hazel metadata such as “Date last matched” and such? And does this only affect the individual file or folder, or all files and folders Hazel is monitoring?

      Sounds like one would need to go in and manually delete this file every time, which would defeat the purpose of automating the process. Unless I am misunderstanding your suggestion?

Leave a comment