Mark Bauer
Mark Bauer

Reputation: 27

Batch custom rename files with applescript

I have hundreds of image files that are currently named:

[LASTname], [firstname].jpg

I need to rename them all:

[firstname]_[LASTname].jpg

So I can't do a simple search and replace or sequential convention or anything like that. I need to copy what comes after the comma, paste it to the front and replace the ", " with a "_"

I am very new to applescript, but it appears that might be a solution. Does any have any ideas on how I could accomplish this?

Upvotes: 0

Views: 352

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207395

I would make a backup first, then do this on a spare COPY of your files in a separate directory.

Save this in your HOME directory as go

#!/bin/bash
shopt -s nullglob nocaseglob

for f in *,*.jpg; do
   base=${f/.*/}      # strip extension
   last=${base/,*/}   # remove comma and anything after
   first=${base/*,/}  # remove anything up to and including comma
   echo mv "$f" "${first}_${last}.jpg"
done

Now start Terminal and make the script executable with:

chmod +x go

Now change directory to where your images are, so if they are in your Desktop in a folder called COPY

cd Desktop/COPY

Then run the script with:

$HOME/go

If the commands look correct, edit the script and remove the word echo near the end and run it again, for real.

By the way, mv is the command to rename a file, so the following changes fileA's name to fileB:

mv fileA fileB

Upvotes: 1

Related Questions