0
votes

Apologies in advance. I am a complete newbie when it comes to programming. I am hoping that someone out there might be able to help me.

Based on the nature of my work, I have to constantly rename a bunch of images. The filenames sometimes vary, but are often some form of chronological numbers displayed in sequential order in their own separate directory (think image001.jpg, etc. but not always that naming format). Anyway, I have to rename all of the files in this particular directory to "000-001.jpg", "002-003.jpg", "004-005.jpg", etc. for every jpg file existing in that single directory. There are usually between 10-999 images in a given directory, but I am rarely given some directories with 1000+ images (in which case I begin naming all files "0000-0001.jpg", "0002-0003.jpg", etc.).

I have been manually renaming probably hundreds of thousands of these images over the course of the last several years. Now I'm finally looking for a way to batch rename them so I don't have to waste hours doing so. It seems that Automator on OSX is the easiest way to go, but Automator cannot rename files in the way I need them on its own.

I did notice that I can employ shell scripts or applescripts as part of an Automator function, so I was wondering if someone might be willing to write a short script for use in Automator (I work much better with a GUI) that would provide the above function? Unfortunately, I have no experience scripting. The absolute best ideal outcome would be for me to use that script within an Automator Application and just drag the files I need renamed onto it. This would bypass my need to open the OSX Terminal, with which I also have zero experience.

As an added note, I am using an older laptop running OSX 10.6.8. I might not have the most updated versions of Applescript, etc. to work from. I would be greatly appreciative if anyone were willing to help me out. Thanks!

-- JE

1
Before anyone can write any code to automate any procrss, they have to understand the objective and rules so you would need to explain how your naming system works. Why does each file need 2 embedded numbers in its name? Does it matter which file gets which name or are they assigned arbitrarily? Are there GIFs and PNGs too? Does any directory (folder) depend on any other?Mark Setchell
The files are images of book pages, two pages per file. Hence 000-001.jpg for pages 0 and 1. It does matter which files get the name, but the files are always in the correct order already, regardless of their preexisting filenames. For example, if I had image01.jpg, image02.jpg, etc., image01 would already be 000-001, etc. Does that help? The files are usually JPGs, and if not, I can rename non-JPG files manually. No directory depends on any other as far as I'm aware. I just want to be able to select all files, drag-and-drop them onto an Automator application, and have it rename them.user5802296
So yeah. It just needs to rename the alphabetically first file 000-001.jpg, the second 002-003.jpg and so on. If there are 1000 or more files in the directory, it should start with 0000-0001.jpg, 0002-0003.jpg and so on. That's about it. The files will already be in the right alphabetical order.user5802296

1 Answers

1
votes

I would do it like this in bash shell because I find Applescript very verbose! The script writes all error messages in a file on your Desktop called "renamerLog.txt".

You must drop a single folder onto the icon.

It copies the files from the specified directory, renaming them as you asked on the way, to a directory (folder) on your Desktop called "Output" which MUST not exist before you start - else it would mix up pages from different books.

It looks like this in Automator:

enter image description here

You can copy and paste the actual script from below:

# Place where we will save messages
LOG="$HOME/Desktop/renamerLog.txt"

# Clear log from any previous run
> "$LOG"

# Set up name for output directory
OUT="$HOME/Desktop/Output"

# Check user dropped a directory, rather than files
if [ ! -d "$1" ]; then
   echo "ERROR: Please drop a DIRECTORY onto me!" >> "$LOG"
   exit 1
fi
echo "Processing files in: $1" >> "$LOG"

# Go to that directory
cd "$1"
if [ $? -ne 0 ]; then
   echo "ERROR: Unable to go to specified directory" >> "$LOG"
   exit 1
fi

# Create output directory
mkdir "$OUT"
if [ $? -ne 0 ]; then
   echo "ERROR: Unable to create output directory" >> "$LOG"
   exit 1
fi

# Don't barf if no files or upper or lower case
shopt -s nullglob
shopt -s nocaseglob

# Count files to determine number of leading zeroes required
for f in *.jpg; do
   ((nfiles++))
done

# Tell user how many files we found and set up output formattiing
echo "Files: $nfiles" >> "$LOG"
format="$OUT/%03d-%03d.jpg"
[ $nfiles -gt 999 ] && format="$OUT/%04d-%04d.jpg"      # Maybe this should be "-gt 499" rather than "-gt 999"

# Now copy the files renaming them as we go
i=0
j=1
for f in *.jpg; do
   newname=$(printf $format $i $j)
   ((i+=2))
   ((j+=2))
   echo Copy $f to $newname >> "$LOG"
   # cp "$f" "$newname"
done

You can try running it a few times and looking at the log to see if you like what it says, then, if you are happy with it, you can remove the # from the penultimate line so that it actually copies the files.

I prefer to do it by copying rather than renaming because I don't want to risk losing your data.