1
votes

I have a large list of images that have been misnamed by my artist. I was hoping to avoid giving him more work by using Automator but I'm new to it. Right now they're named in order what001a and what002a but that should be what001a and what001b. So basically odd numbered are A and even numbered at B. So i need a script that changes the even numbered to B images and renumbers them all to the proper sequential numbering. How would I go about writing that script?

images list

2
I am an avid user and answerer of questions on Stack Overflow. I'm not attempting to get free work. I've never used Applescript before and don't know where to start. Normally for this sort of function i use Automator but automator has no way of doing if..then statements. - Ryan Poolos
All of the 02s go to 01b and all of the 03s go to 02b? - adayzdone
what001a is fine. what002a should be what001b. what003a should be what002a, what004a should be what002b. - Ryan Poolos
If what003a should be what002a, what happens to the original what002a? - adayzdone
I've got an answer to post but I have to wait one hour before I can post it. - Ryan Poolos

2 Answers

2
votes

A small Ruby script embedded in an AppleScript provides a very comfortable solution, allowing you to select the files to rename right in Finder and displaying an informative success or error message.

The algorithm renames files as follows:

number = first 3 digits in filename                # e.g. "006"
letter = the letter following those digits         # e.g. "a"
if number is even, change letter to its successor  # e.g. "b"
number = (number + 1)/2                            # 5 or 6 => 3
replace number and letter in filename

before and after

And here it is:

-- ask for files
set filesToRename to choose file with prompt "Select the files to rename" with multiple selections allowed

-- prepare ruby command
set ruby_script to "ruby -e \"s=ARGV[0]; m=s.match(/(\\d{3})(\\w)/); n=m[1].to_i; a=m[2]; a.succ! if n.even?; r=sprintf('%03d',(n+1)/2)+a; puts s.sub(/\\d{3}\\w/,r);\" "


tell application "Finder"

    -- process files, record errors
    set counter to 0
    set errors to {}
    repeat with f in filesToRename
        try
            do shell script ruby_script & (f's name as text)
            set f's name to result
            set counter to counter + 1
        on error
            copy (f's name as text) to the end of errors
        end try
    end repeat

    -- display report
    set msg to (counter as text) & " files renamed successfully!\n"
    if errors is not {} then
        set AppleScript's text item delimiters to "\n"
        set msg to msg & "The following files could NOT be renamed:\n" & (errors as text)
        set AppleScript's text item delimiters to ""
    end if
    display dialog msg
end tell

Note that it will fail when the filename contains spaces.

1
votes

A friend of mine wrote a Python script to do what I needed. Figured I'd post it here as an answer for anyone stumbling upon a similar problem looking for help. It is in Python though so if anyone wants to convert it to AppleScript for those that may need it go for it.

import os
import re
import shutil

def toInt(str):
  try:
    return int(str)
  except:
    return 0

filePath = "./"
extension = "png"

dirList = os.listdir(filePath)

regx = re.compile("[0-9]+a")

for filename in dirList:
  ext = filename[-len(extension):]
  if(ext != extension): continue
  rslts = regx.search(filename)
  if(rslts == None): continue
  pieces = regx.split(filename)
  if(len(pieces) < 2): pieces.append("")
  filenumber = toInt(rslts.group(0).rstrip("a"))
  newFileNum = (filenumber + 1) / 2
  fileChar = "b"
  if(filenumber % 2): fileChar = "a"
  newFileName = "%s%03d%s%s" % (pieces[0], newFileNum, fileChar, pieces[1])
  shutil.move("%s%s" % (filePath, filename), "%s%s" % (filePath, newFileName))