Regular Expressions

Regular expressions (learn more about them here) can be used in two spots in Usher. If you don't know regular expression syntax, the following won't be of much help to you, other than pointing out that some things are possible using regular expressions.

The first way to use regular expressions is when populating Properties in Usher's metadata editor—a special mode lets you parse filenames for values to use in other metadata fields. The second way to use regular expressions—plus a couple of Usher-only extensions—is when doing batch file renaming.

 

Properties

Typically when adding a property to a media file, you create the property, then type (or paste) in the value for that property. But Usher includes a Name: Regular Expression entry in the Import sub-menu, which is available on the menu that appears when you click the plus sign to add a new value to a property.

This feature lets you parse your filenames for data to use in other property fields. Here's an example; assume a collection of public domain movies included the release year as part of the filename, as seen here:

Names with dates

It'd make more sense to use the Year property to store these values, instead of keeping them in the filename—you could then easily build a Smart Playlist based on year, for example. Enter Name: Regular Expression to ease the task.

Select all of the movie files, then use FileEdit Metadata (or press Command-T) to open the metadata editor window. The first step in using Name: Regular Expression is to make sure there's a property to hold the value—in this case, Year. Year is a built-in property in Usher; click the plus sign at the lower left of the window and choose Year from the pop-up menu. (If you're using a non-default property, just choose New… from the pop-up menu instead.)

Click the plus sign at the right of the new field, then select ImportName: Regular Expression from the pop-up menu.

Import via name regex

Select the new blue pill that appeared, then double-click it to edit, so it looks like this:

Editing the property

You can now edit the regular expression to parse the filename as you wish. For this example, we'd want to strip everything other than the year at the end of the filename, which could be done with this construct:

<?meta:Name: ^.* - (.*)$?>

If you use capture groups in the regular expression, as seen here, then Usher will only keep those groups, discarding everything else. If you capture more than one group, they will be merged into one.

After making the change, press Return to set the new value, then click OK. The rule will then be applied to the selection. Reopen the metadata editor and you'll see the results:

Year property added from name

Depending on how your files are named, you could also use this import mode to populate Properties such as season and episode numbers.

Now that the year is properly stored as a property, you don't need it in the filename…enter the second spot where you can use regular expressions in Usher.

 

Batch Renaming

In Usher's batch renaming mode, which you can access via the metadata editor or the File menu, allows the use of regular expressions in its "Find and replace" renaming mode. Using a regular expression, it's easy to remove the extraneous date from the filenames:

Remove year from name

This is basically the reverse of the expression that extracted the year. As before, the group capture part is retained, which is just the filename in this case, and everything else is dropped. Using regular expressions in "Find and replace," you can do some very powerful renaming.

Usher uses Apple's built-in regular expression API; this section of Apple's API page shows the syntax and commands.

But there are a couple of useful things you can do—beyond regular expressions—when working with batch renaming in Usher.

A simplified 'else' statement

If you're using Usher's properties in your renaming action—say a property named Series for television shows—then you can perform a very simple conditional check. Consider this scenario: You want to append the series name to each media file's name, but not everything is part of a series.

Start a batch renaming operation and choose "Add prefix or suffix" as the action. In the Suffix box, click the plus sign and choose Series; you may optionally add separators (space-hyphen-space) as shown in the screenshot below. When Series appears in the box, click and edit it, and add |"Other", like this:

Simple else

The pipe (vertical bar) symbol means that if there's no value for the first item (the series name), then Usher will use the value after the pipe. And in this case, that value is in quotes, which means that Usher will insert the word Other. Without quotes, Usher would try to insert a property named Other, which is not the desired outcome.

When done editing, press Tab to exit the field, and the text will change to simply Series|"Other". Click OK, and your files will be renamed—and those without a series value will have - Other appended instead of the series name.

You can also chain several pipes, and you can combine values via + within each of those. For example, this…

<?meta:Series+" - "+Episode|"Banana"?>

…would result in something along the lines of 1 - 1 for a file that has defined values for both a Series and Episode property, but it would result in Banana for a file that lacks Series or Episode. In other words, a plus-separated value is skipped as soon as one of the metadata placeholders in it provides no value.

If, for example, you want to use Episode values regardless of whether Series exists as well, you'd use this version:

<?meta:Series+" - "?><?meta:Episode|"no episode"?>

And if you still wanted Banana if neither Episode nor Series existed, you could do this:

<?meta:Series+" - "+Episode|Episode|"Banana"?>

Between regular expressions and this simple version of an 'else' statement, you can do some truly powerful renaming in Usher.