
So we have now automagically converted an incoming file into an iTunes-friendly format. If you followed my Hazel rules to the letter, you should have also found that the original avi file has been automatically trashed too. So, what's next?
Step 3: Rename Files
Oh boy, this step is a bit of a doozy. The problem (and it is a big one), is that nobody uses exactly the same naming format for TV Shows. There are a million different variations out there. So for me, these rules are changing & evolving all the time. Which annoys me, because I'd like to make this all as automatic as possible. So I will say this these steps will accommodate most naming schemes, but perhaps not all.
Move your files to a sorting folder
The first step in this process is to move your iTunes-friendly files to a folder where you can have a set of rules that look at specifically at renaming your show properly. So, let's create a new folder to do this. Create a new folder inside your TV Shows folder, called About to be Sorted.

Now we're going to head back to Hazel to create a rule to move your files.

Rule: is TV (<1GB)

This first rule examines a set of criteria to determine whether the file is good to go:
- is the file of type m4v?
- was the file created by our encode workflow? (using the spotlight comment 'Encoding Complete')
- was the file not modified in the last 5 minutes? (this helps make sure that it doesn't touch files that are still being moved)
- is the file under 1GB in size?
If a file meets all these criteria - sweet! It's ready to be moved to your renaming folder. Set your rule to move the file to the About to be Sorted folder.
Sidenote: I generally use filesize as the way to work out whether an encoded file is a movie or a TV Show. I used to use other methods (like whether the filename had an [S0XE0X] bit in it), but it was too unreliable. The only time this goes wonky is when TV shows go over 60 minutes, or movies go under 60 minutes.
Modifying this part to accommodate for movies means simply adding another rule to this area to shoot movies into a different sorting folder. Don't worry movie buffs, I will specifically cover organising movies in a later tutorial.

All being well, our iTunes-friendly copy should now shoot across into our About to be Sorted folder:

From there, it's ready to be renamed. Let's look at what Hazel can do for us.

Oh....crap. That's a lot of rules! Don't worry though, this is where all the magic happens! Once it's up and running, it will bring a tear to your eye, I swear.
Firstly, let's look at the Rename rules. The other rules form part of Step 4, which we'll cover in Part 3 of this tutorial.
It's all about patterns
The key to creating rules that can do renaming effectively is pattern-matching. Each rule in turn asks the file, "So, do you match this type of naming scheme?" If it does, it can:
- analyse the name,
- break it down to it's component parts,
- successfully rename the file.
If not, it just moves on to the next rule and tries to match it against a different pattern. It continues all the way down the list, trying to match up with any of our patterns. Let's look at the first rule in depth.
Rule: Rename -- [Show].S[S]E[E].[...]

OK, so as I said above, this rule is basically just trying out a pattern on our current filename. The key to this is expanding the Patterns dropdown, where we can work with Hazel's totally sweet custom tokens. Custom tokens are the key to breaking down a file name to it's essential component parts that we need to know. Those parts are:
- the TV Show's name
- the Season number
- the Episode number

If you drag and drop a custom token into your naming pattern, you can start to set up your own tokens. Here's the first one:

Tokens can be made up of a single letter/character/digit, or many letters/digits/characters, or even anything at all. This helps Hazel recognise what part of this filename is specifically the Show's name, separate from all the other crap in the name.
Now, this is a really important bit. You can't see it very well in the screenshot because Hazel clips it, but in this rule, I've set up a matching pattern like this. Anything in square brackets are custom tokens.
[Show].S[Season]E[Episode].[...]
This matching pattern is looking specifically for any characters at all that occur before a full stop and a capital S. That set of characters becomes the show's name.
Next, the matching pattern looks for a Season custom token. The Season custom token is set up like this:

[Show].S[Season]E[Episode].[...]
So the matching pattern is looking for any numbers that occur between a full-stop and capital S, and a capital E. Those numbers become the Season number.
Finally, here's the Episode custom token:

[Show].S[Season]E[Episode].[...]
Very similar to the Season token, this token is looking for a number between a capital E, and another full-stop. Those numbers become the Episode number.
Finally, the last part of our pattern is an 'Anything' token:

[Show].S[Season]E[Episode].[...]
This filters off the crap at the end of the filename that we don't need.
So, let's look at our example.
Arrested.Development.S01E01.HDTV.XviD-SYS.m4v
Our totally awesome pattern-matching algorithm would recognise:
- the Show's name is Arrested.Development
- the Season number is 01
- the Episode number is 01
- the unwanted crap at the end is HDTV.XviD-SYS
Now we have all the component parts we need, we can start to reconstruct this filename the way we'd like it to appear. The action looks like this:

The format I'd like my files to be renamed into looks like this:
[Show] - S[Season]E[Episode]
What I love about Hazel is that we can now use the custom tokens we've created to reconstruct the filename. So to create this action, simply drag and drop the tokens you've already created into your rename pattern.
Once you've done that, your filename should go from this:
Arrested.Development.S01E01.HDTV.XviD-SYS.m4v
to this:
Arrested.Development - S01E01.m4v
Still not quite perfect though... there is still a full-stop between the Arrested and Development parts in the Show's name.
This is where another Automator workflow comes in!

(click to enlarge)
This leverages Automator to process any movie filename, and finds any full-stops in a base filename, and replaces them with spaces. This workflow is run immediately after the file is renamed:

Again, for those of you who haven't used Automator before and would like more information on creating this workflow, check out Matt's excellent article on*Getting Your Feet Wet With Automator. It's just plain lovely.
At the end of all this, our filename should have gone from:
Arrested.Development.S01E01.HDTV.XviD-SYS.m4v
to...
Arrested.Development - S01E01.m4v
to...
Arrested Development - S01E01.m4v
Frickin' awesome.
Rules & rules & rules
All the other rules in this step around renaming are just variations on this theme. For example, a file might follow this kind of naming format.
Doctor.Who - 5x3 - Victory of the Daleks
That kind of naming scheme would need a pattern-match like this:
[Show] - [Season]x[Episode] - [...]
and then a 'Rename File' pattern like this:
[Show] - S0[Season]E0[Episode]
I won't go into intricate detail on all the possible pattern-matches in this post, but I'm happy to share some of the ones I've found in the comments.
The main point is that as long as you have a bank of pattern-matches in your Hazel rules, then a file can merrily go from rule to rule, trying to match up. When it finally does, it can come out in a totally standard way.
Summing up

This is the second step in a set of tutorials on sorting media automatically with Hazel. At the end of this tutorial you should be able to move iTunes-friendly files into a sorting folder, create a pattern-matching algorithm with Hazel's custom tokens, and successfully rename your files to a standard pattern. Stay tuned for the next article, where I look at automatically tagging your files with iTunes-friendly metadata, artwork & sorting them for storage!







Organising media with Hazel, Pt 2