MacOS – Clean up file names using Automator

automatorfindermacos

I’m fairly new to Automator, in that I’ve used it only once before. What I’m trying to do is clean up a bunch of Android app assets, which are named like so:

01_something
1_something_else
002_something_too

What I want to do is remove the leading digits + underscore, i.e. any leading non alphabetical characters. I imagine this can be done either by having a script that’s smart enough remove them, perhaps by some manner of regex, or by having a prompt that asks me how many characters I want to remove, if Automator even supports that.

Secondly, I’d like to add a prefix to the files, like tile_ or ab_. Again, if I could select some files in Finder, and choose ‘Add Prefix’ or something, and enter some text into a prompt, that’d be optimal.

Best Answer

There is no need for dedicated file renaming software to do such relatively simple batch renaming operations – Automator will allow you to create services that do what you want.

Your first task (removing a numerical prefix followed by an underscore form file names) can be solved like this:

  1. create an Automator service (choose “Service” when prompted what kind of workflow to create by Automator). Set it to take “Files or folders” as input.
  2. add a “Filter Finder Items” action and set it to filter on the condition “none” match “kind is Folder” to make sure you have only files.
  3. Add a “Run Shell Script” action and make sure it runs /bin/bash and takes its input “as arguments”. Insert the following code:

    shopt -s extglob
    for f in "$@"; do
        fpath="${f%/*}"
        fname="${f##*/}"
        mv "$f" "$fpath"/"${fname/#+([[:digit:]])_/}"
    done
    

    – see the Bash Reference Manual’s chapter on Pattern Matching for an explanation of how this works (recommended reading for this kind of operation anyway :)).

  4. save to the default location as “Remove Prefix”.

– you will now have a “Remove Prefix” in the Service menu of Finder (and the context menu of files and folders) which will rename files so a numerical prefix followed by an underscore is stripped from the file name.

The second task (prefixing the files with a selectable string) is even easier to achieve, using a Standard Automator action:

  1. create an Automator Service as in the steps 1 and 2 above.
  2. add a “Rename Finder Items” action. Set it to “Add Text”, enter a default prefix in the text box (if you like), set the string to be applied “before the name”. Select “Options” in the bottom row of the action and tick “Display this action when running” (disclaimer: I’m on a German system – all translations approximative).
  3. save to the default location as “Add Prefix”.

– when you run the service on one or more files, you will see the following dialog:

Automator rename option dialog

(or rather, its equivalent in your system language), where you can edit the prefix you want to use before applying it.

Happy renaming with OS X’ on-board abilities :).