Hazel Rule (or AppleScript) to rename files from numbers

Hi gang -

Since starting to get back into automation and setting up workflows I wonder how I might best do this.

I work in Numbers a lot and export to CVS files. This will place a folder on my desktop with all the various sheets included. Numbers adds “Table 1” to each of the sheets, so I get a folder with my file name (the folder name changes each time, and takes the name from the Numbers Doc), and inside I get files names such as:

Albums-Table 1.csv
Composers-Table 1.csv
Registrations-Table 1.csv

What I would like to do is have a Hazel rule to strip away the “-Table 1” from each of the file names. I can’t figure out how to get hazel to do this. I know how to make it “watch” the desktop for a folder. And I see there is a Run Rules on Folder Contents action. But nothing I try can get it to work.

Any thoughts?

Thanks
Kayle

Set up a rule that looks for filenames containing “-Table 1” (without the quotes):

Have that rule rename the file. Click on the “name” token, then click on the little arrow next to “name” and select “Replace text…”

In the box that pops up, enter “-Table 1” (without the quotes) in the first box and leave the second box blank.

When this rule runs it should rename files the way you want.

Thanks Chris -

that is exactly what I tried, but it doesn’t work as the files are contained in a folder. That folder is aways a different name, so I can’t search on folder name. I also tried to make the Kind a Folder for the first step, but that doesn’t do it either.

I think that hazel is looking for a Folder with that criteria instead of know to look inside any folder on the Desktop and than perform the rules.

I tried adding the Name condition after the Kind rule, but that doesn’t work either!

You need two rules, both attached to your desktop folder. One rule is just a rule that looks for folders and does the “Run rules on folder contents” action.

The second rule just renames the file, as described above.

1 Like

Perfect!! Thanks Chris. That does the trick.

Now, is it possible to add a wild card? Most of time the files will end with “-Table 1.cvs” But sometimes, it might be “-Table 2.cvs”.

I tried to use “-Table *” but that didn’t work. It’s ok if not, its a rare use case

Cheers!

Use “matches” rather than “contains”. You’ll want a “Characters” widlcard, followed by "-Table ", a “Digit” wildcard, and “.csv”

I don’t know that there’s any way to use wildcards in the Rename, so you may need multiple statements there.

1 Like

That’s great. Thanks. I can’t seem to wrap my mind around using multiple conditions and statements. Can’t teach an old dog and all that…

Cheers!

@ChrisUpchurch has a great start for you here but there’s one problem and a limitation: the rules specified won’t match if you’ve got a space or other special character in the text before the -Table and you’ll need to add an exception for each -Table (number).

To make this a little more robust, you can extract the ‘True File Name’ and use that in your actions to rename the file (rather than just replacing the offending bits).

Conditions

For your conditions, start with an Extension: is with csv so you don’t accidentially get another file, however unlikely it may be.

Then keep the Name: matches rule, and drag in a custom text token. You can name it (I called it True File Name) and tell it to look for anything. Then in the main pattern, add -Table and drag in a Number [123] token. You can see this below:

Actions

The • True File Name token can then be used in your Rename action followed by the file extension:

And this should be the resulting Hazel rule:

I hope this is useful to you.

2 Likes

Thanks - that is great. May I ask how this works? The True File Name token includes anything, which I assume would include the entire file name. So once I add the “-Table” and number token, that removes that info from the True Name token (so in this case, it makes the variable become what ever is in front of -Table)?

This is pretty cool.

Thanks!!

Yes, you understand correctly. :slightly_smiling_face: True File Name’s anything token will gobble up everything up to the point it meets ‘-Table [number]’, even if it were Cheese and Card-Table-Table 75.csv to get Cheese and Card-Table.

I get it. What if the file name was cheese and card-Table-Table?

If there’s no number, the rule won’t match at all.

1 Like

Perfect. Thanks!

One more please? Can I do something like this as a rule?

True file Name-Table-Something Else NumberToken? So, can I keep adding things for different file names?

You could certainly do this, but you might want to adjust it depending on what your desired outcome is. But speaking hypothetically, yes, it’s definitely possible.

1 Like

Thank you for helping me understand this!

1 Like

You’re welcome! It’s fun! :smiley:

Cool.

As I get more into what Hazel/Drafts/Workflow can do, I see more ways I can use it. I’m sure I will be hitting you up again. :grinning:

1 Like