A Bulk File Renaming Workflow for Alfred

iconDo you know Alfred? Alfred is one of the greatest productivity apps for Mac. And it becomes better and better with each version.

One main reason for its greatness is the powerful workflow feature which allows us to add new functionality by providing a new workflow.

I use custom workflows for many things such as storing some customized affiliate links or to control my openHAB via some key strokes. Workflows are a great possibility to add features to your Mac that it has not by default.

Recently, I missed a bulk renaming feature which allows me to rename multiple files at once by a given pattern. Of course I could have used the shell and some regular expressions. But wouldn’t it be cool to select files in the Finder and rename them just in place?

Naturally, an Alfred workflow was my first choice for this task.

Since now, afaik, there does not exist such a workflow and hence, I decided to develop one on my own. On a nice Sunday afternoon, I dived deeply into the Alfred workflow feature and figured out how such a bulk renaming workflow could look like.

The Bulk Renaming Workflow from a Users Perspective

I decided to create a workflow using external python scripts so that I can publish the workflow on GitHub.

It was pretty clear to me, that the workflow must be “File Action” based. That is, it must be triggered by selecting files in Alfred or in the Finder triggering Alfred file actions.

Alfred File Actions
When double-pressing “Alt” in Finder, Alfred comes up with a number of actions I can execute with the selected files.

As you can see, the second option is “Rename files”. This option is provided by my workflow. When selecting it, my workflow guides the user through the renaming process.

The Matching Pattern

Initially, the user needs to enter or choose a regular expression that is used to get parts of the file names. The workflow only cares about the file name and not the suffix. Hence the expression must match the name only.

Bulk Renaming Workflow - Match Regex
The workflow allows you to choose some predefined regular expressions or to enter a custom one.

The Replace Pattern

After pressing enter again, a new dialog comes up asking the user for a replace pattern. There are several options for this pattern:

  • $0 yields the entire file name (good for adding a serial number)
  • $1 – $9 give the contents of the match groups defined previously
  • {n} gives a serial number counting from 1 up to the number of renamed files
  • {d} gives the current date in the format ‘YYYY-DD-MM’
  • You can prepend the following options to the replace pattern:
    • c to capitalize every word in the name
    • r(<from>,<to>) to replace every occurrence of <from> with <to>
    • The options are separated by a / from each other and the pattern itself. You can add multiple options. I.e. r(-, )/c/$0 will replace every dash with a space and capitalize every word.

Again, you may choose a predefined pattern or enter your own.

Bulk Renaming Workflow - Replace Pattern
The user can choose a predefined replace pattern or enter a custom one.

The Replacement Preview

After choosing a replace pattern and pressing enter again, the workflow presents a preview to the user. Here she can press enter to continue or ESC to abort the renaming process and leaving the files unchanged.

Bulk Renaming Workflow - Preview
The users gets a list of all files that will be renamed. In the title she can see the old name and in the subtitle she can see the new name.

In the screenshot above, I had three files demo-A.jpg, demo-B.jpg and demo-C.jpg. I have chosen “Prepend current date” in the second dialog. In this case, the selection in the first dialog was completely irrelevant since $0 yields the entire file name, no matter what regex I have defined as matching pattern.

After pressing enter, all three files are renamed and a notification is posted to the OS X Notification Center, stating that the process has finished successful.

The Bulk Renaming Workflow from a Developers Perspective

After figuring out how to develop Alfred workflows, the creation was straight forward. My workflow is compatible with Alfred 3.0.2 and upwards.

Bulk Renaming Workflow - Workflow View
This is the workflow as it looks in Alfred’s workflow editor.

Normally, the output of every step is passed as input to the next step. This is not what you want if you have multiple steps which are independent from each other, e.g. the file action and the pattern dialog. But you can avoid this behavior by adding a “Args and Vars” step in between. This item allows you to store the output of the previous step into an environment variable, valid for the entire workflow, and to pass nothing to as input to the next step.

Hence, after every step, I store its output into an environment variable. In the following, I will explain the workflow step-by-step.

  1. The first step “Rename files” is a File Action that has a tab-seperated list of input files as output.
  2. The “Enter Match Pattern” step is a “Script Filter” which allows you to provide a list of options to Alfred (the options you can see below the search field in the second picture above). The first option just contains the input, given by the user. This allows the workflow to accept any input.
  3. The “Enter Replace Pattern” step is a “Script Filter” again. In fact it is very similar to the previous step but with different options.
  4. The next step “External Script” just builds a mapping between the old and the new file path.
  5. The “Confirm Replacement” step presents the list of replacements to the user.
  6. The last “External Script” does what remains to do: Renaming all files as displayed previously. Its output (a message) is passed to the “Bulk File Rename” step which only posts a notification to the Notification Center.

Every step is contained in its own python script. You can see all the code in the GitHub repository. Also the icon and the Alfred workflow configuration is stored there.

The Bulk File Renaming Workflow is also available on Packal.

Contribution Welcome!

If you have some cool ideas for this workflow, feel free to comment, add an issue on GitHub or provide a pull request.

6 thoughts on “A Bulk File Renaming Workflow for Alfred”

  1. “Krojamsoft BatchRename” Tool is a powerful tool, that allows you to quickly rename all the files in a specified directory. You can remove spaces, replace spaces with underscore, uppercase/lowercase filename, add a prefix/suffix, remove/replace strings and also catalog files by adding an incremental number to the file name.

    1. This is a setting in Alfred: Features -> File Search -> Actions
      At the bottom, you can enter a File Selection hotkey.

Leave a Reply

Your email address will not be published. Required fields are marked *