简体   繁体   中英

Creating a batch file-renaming script for use in OSX Automator

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

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 :

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM