Can’t Rename Images Sequentially Via Applescript Because Other Images Already Have That Name

applescriptfinderhazelterminal

I am trying to automate batch renaming of images sequentially by using a simple Applescript. But I am having difficulty telling the script that there are images in the same folder that already have the same name.

For example, I have three images of a cat that are named Hello World 1, Hello World 2 and Hello World 3.

In the same folder I also have three more of the same image but with the name Test Image 1, Test Image 2 and Test Image 3.

enter image description here

I want to rename all of the "Test Images" to Hello World 4, Hello World 5 and Hello World 6 but I get the following error:

error "Finder got an error: The operation can’t be completed because
there is already an item with that name." number -48

How do I tell the script that there are other images in this folder that have the same name and that it needs to start from "Hello World 4" and continue renaming the rest of the images?

Please note that I will be using this as an embedded script in an application called Hazel (noodlesoft.com) and it does not accept handlers. So I will need a solution that does not use a handler.

Here is the script I am using that gives me the error when I try to rename the "Test Images":

tell application "Finder"
    set all_files to every item of (choose file with prompt "Choose the Files you'd like to rename:" with multiple selections allowed) as list
    set new_name to "Hello World"
    repeat with index from 1 to the count of all_files
        set this_file to item index of all_files
        set {itemName, itemExtension} to {name, name extension} of this_file
        set index_prefix to " "
        if itemExtension is "" then
            set file_extension to ""
        else
            set file_extension to "." & itemExtension
        end if
        set the name of this_file to new_name & index_prefix & index & file_extension as string
    end repeat
end tell

Best Answer

The main problem is similar to your other topic - the new name + suffix is not checked against what is already in the folder. My answer to that topic used a general-purpose handler for getting a new name, so without using that, the (stripped down) contents are used instead:

set all_files to (choose file with prompt "Choose the Files you'd like to rename:" with multiple selections allowed)
set theName to "Hello World " -- includes separator text

tell application "Finder"
    set theFolder to container of first item of all_files -- the base folder
    repeat with this_file in all_files
        set theExtension to name extension of this_file -- keep existing extension
        if theExtension is not "" then set theExtension to "." & theExtension
        if name of this_file does not start with theName then -- don't rename renamed items

            # the "increment suffix number until the name is unique" part
            set counter to 1 -- starting suffix
            set newName to theName & counter & theExtension
            tell (get name of items of theFolder) to repeat while it contains newName -- get new name
                set counter to counter + 1
                set newName to theName & counter & theExtension
            end repeat

            set name of this_file to newName
        end if
    end repeat
end tell
Related Question