Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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 :).

share|improve this answer
    
@kopishke: Great thanks! Any recommended resources for better learning Automator? –  Josh May 31 '12 at 21:22
1  
Mac OS X Automation is a good starting point. –  kopischke May 31 '12 at 21:26

I've always found the commercial app A Better Finder Rename a time-saver when trying to work out complex actions like this.

Last year I had a humongous set of PDF files (>25k individual files!) which needed their filenames changed from FirstName,LastName.pdf to LastName_FirstName.pdf. Although this is a relatively simple filename mod, ABFR has far more advanced features which will allow you to do what you need.

What made the job even easier was the very cool mechanism whereby ABFR can create a droplet from your renaming specifications. Drop a folder or file on the droplet - ABFR processes the files in a flash.

(Disclosure: I'm a very satisfied customer of this software, and have no financial interest in ABFR.)

share|improve this answer
    
OP asked about an Automator solution. Recommending 3rd party software would only be acceptable if Automator provided no way to achieve this, but it does – see my answer. –  kopischke May 31 '12 at 19:20
    
Not to put too fine a point on it, kopische, but Automator itself cannot do the operation Josh requested. Your shell script, invoked from within Automator, does the heavy lifting. Perhaps Josh, like I, cannot write shell scripts with your flair and elan, and prefer to use an app which does the work quickly and quietly. I, too, would like to learn more shell scripting techniques, but I have limited time/desire, capisce? –  IconDaemon Jun 4 '12 at 15:49
    
No to put too fine a point on it, but Automator does half of what OP asks by using GUI configurable actions (prefixing the files with a selectable string), if that is what you mean “by itself”. I’d also argue that the AppleScript and shell script actions come with Automator, so they count as “by itself” too, but that is a matter of semantics. Anyway, this was no criticism of your preferences in software (I use Name Mangler myself) – just my opinion that on Stack Exchange, we should all try to stick to the question asked as closely as possible. –  kopischke Jun 4 '12 at 20:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.