• In my last article, I looked at 2 steps of a five-step process for automatically organising media with Hazel. Here's where Part 1 ended:

    The End of Hazel Tutorial, Part 1


    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.

    Create a new folder called About to be Sorted

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

    isTVrule

    Rule: is TV (<1GB)


    isTV.png

    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:

    Ready to be renamed

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

    oh boy, there's a lot of rules there

    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:

    1. analyse the name,

    2. break it down to it's component parts,

    3. 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].[...]


    Rename rule 1

    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


    customtokens.png

    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:

    Defining a Show name token

    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:

    Custom token to match season

    [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:

    The custom token for episode number

    [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:

    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:

      Rename the file using your custom tokens

      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!

      Automator scans the file and renames it

      (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:

      Automator workflow is launched immediately after Hazel renames the file

      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


      Final Product at the end of part 2

      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!
  • New Forum Posts

    bennyling

    Bjango actually has a few small games of their own suitable for kids: Matches, Kapowie, maybe even Sideways Racing.

    All are simple-gameplay

    What apps would you suggest to parents?

    bennyling Today, 02:18 PM Go to last post
    Jamesvdm

    Matching Zoo

    For the really little ones. It's just memory, with a couple of levels of difficulty.

    Plants vs Zombies

    What apps would you suggest to parents?

    Jamesvdm Today, 02:07 PM Go to last post
    TTGSteve

    In my small town, I’m basically known as “the computer guy”. Therefore, if it involves some sort of technology, I’m the go-to-guy for a lot of people.

    What apps would you suggest to parents?

    TTGSteve Today, 01:32 PM Go to last post
    swoffa

    Draw me something

    Random Photography Thread Version IV

    swoffa Today, 01:30 PM Go to last post
    Marcsteel

    I've been sharing my hotspot with my family who have an iPad 1 wifi only so they can access my 3G. As others have said, I have Telstra's 365 day prepaid

    iPad 4G is missing Personal Hotspot enablement

    Marcsteel Today, 01:25 PM Go to last post
    gehenna

    You mean an app you've developed?

    App Downloads

    gehenna Today, 01:08 PM Go to last post
    Mac Monk

    Hi All

    Does anyone know how to find out the total number of times your app has been downloaded since day one, going back two years?

    App Downloads

    Mac Monk Today, 12:52 PM Go to last post
    kyte

    Its looking good, and its very tempting, even for those of us who already have a K-5

    Pentax K-30 Camera - Hands-On Preview

    Pentax K-30 Official

    kyte Today, 10:55 AM Go to last post
    fulltimecasual

    Like i said on the podcast, sometimes its really handy.. Like if you're stuck in a long meeting without wifi and you need to use your laptop, or if you're

    iPad 4G is missing Personal Hotspot enablement

    fulltimecasual Today, 10:48 AM Go to last post
    kyte

    Sorry, Steve, I won't ship to the US. Craigslist was a washout, did a test ad for a small item and got idiots. I know it goes great in the US but it

    Best Place to sell Camera Gear

    kyte Today, 10:30 AM Go to last post