Can't Rename Images Via Applescript Because Same Name Exists

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. You can view it here: https://i.stack.imgur.com/R9zxb.jpg

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:

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

Just put into try block:


try
	set the name of this_file to new_name & index_prefix & index & file_extension
end try

This way error will skip renaming the current file. As string here can be omitted. AppleScript is smart to understand that you want string. It is because the first operand of & operation is string “Hello, word”.

Then,

  1. choose file should be outside of tell application “Finder” block.
  2. The variable index is reserved word, so you should use some other variable name, as i, or k.

set all_files to (choose file with prompt "Choose the Files you'd like to rename:" of type "public.image" with multiple selections allowed)

tell application "Finder"
	repeat with i from 1 to count of all_files
		set aFile to item i of all_files
		set anExtension to name extension of aFile
		try
			if anExtension is "" then
				set the name of aFile to "Hello, World " & i
			else
				set the name of aFile to "Hello, World " & i & "." & anExtension
			end if
		end try
	end repeat
end tell

@KniazidisR
For sure, applying the name “Hello, World” when some files are already named “Hello World” will not issue an error.

@kenexzo
Which files were you selecting?
If I select the 3 files whose name start with “Test Images”, I get an error.

If I select every picture files I get a correct result.
To test I ran :

set theFolder to ((path to desktop as text) & "cats:") as alias
tell application "Finder"
	set all_files to (choose file with prompt "Choose the Files you'd like to rename:" of type "public.image" default location theFolder with multiple selections allowed)
	
	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

The History returned :


# This is not a script. Dont' click “Open this Scriplet in your Editor:”

tell current application
	path to desktop as text
		--> "SSD 1000:Users:**********:Desktop:"
end tell
tell application "Finder"
	choose file with prompt "Choose the Files you'd like to rename:" of type "public.image" default location alias "SSD 1000:Users:**********:Desktop:cats:" with multiple selections allowed
		--> {alias "SSD 1000:Users:**********:Desktop:cats:Hello World 1.jpg", alias "SSD 1000:Users:**********:Desktop:cats:Hello World 2.jpg", alias "SSD 1000:Users:**********:Desktop:cats:Hello World 3.jpg", alias "SSD 1000:Users:**********:Desktop:cats:Test Images 1.jpg", alias "SSD 1000:Users:**********:Desktop:cats:Test Images 2.jpg", alias "SSD 1000:Users:**********:Desktop:cats:Test Images 3.jpg"}
	get name of alias "SSD 1000:Users:**********:Desktop:cats:Hello World 1.jpg"
		--> "Hello World 1.jpg"
	get name extension of alias "SSD 1000:Users:**********:Desktop:cats:Hello World 1.jpg"
		--> "jpg"
	set name of alias "SSD 1000:Users:**********:Desktop:cats:Hello World 1.jpg" to "Hello World 1.jpg"
		--> "Hello World 1.jpg"
	get name of alias "SSD 1000:Users:**********:Desktop:cats:Hello World 2.jpg"
		--> "Hello World 2.jpg"
	get name extension of alias "SSD 1000:Users:**********:Desktop:cats:Hello World 2.jpg"
		--> "jpg"
	set name of alias "SSD 1000:Users:**********:Desktop:cats:Hello World 2.jpg" to "Hello World 2.jpg"
		--> "Hello World 2.jpg"
	get name of alias "SSD 1000:Users:**********:Desktop:cats:Hello World 3.jpg"
		--> "Hello World 3.jpg"
	get name extension of alias "SSD 1000:Users:**********:Desktop:cats:Hello World 3.jpg"
		--> "jpg"
	set name of alias "SSD 1000:Users:**********:Desktop:cats:Hello World 3.jpg" to "Hello World 3.jpg"
		--> "Hello World 3.jpg"
	get name of alias "SSD 1000:Users:**********:Desktop:cats:Test Images 1.jpg"
		--> "Test Images 1.jpg"
	get name extension of alias "SSD 1000:Users:**********:Desktop:cats:Test Images 1.jpg"
		--> "jpg"
	set name of alias "SSD 1000:Users:**********:Desktop:cats:Test Images 1.jpg" to "Hello World 4.jpg"
		--> "Hello World 4.jpg"
	get name of alias "SSD 1000:Users:**********:Desktop:cats:Test Images 2.jpg"
		--> "Test Images 2.jpg"
	get name extension of alias "SSD 1000:Users:**********:Desktop:cats:Test Images 2.jpg"
		--> "jpg"
	set name of alias "SSD 1000:Users:**********:Desktop:cats:Test Images 2.jpg" to "Hello World 5.jpg"
		--> "Hello World 5.jpg"
	get name of alias "SSD 1000:Users:**********:Desktop:cats:Test Images 3.jpg"
		--> "Test Images 3.jpg"
	get name extension of alias "SSD 1000:Users:**********:Desktop:cats:Test Images 3.jpg"
		--> "jpg"
	set name of alias "SSD 1000:Users:**********:Desktop:cats:Test Images 3.jpg" to "Hello World 6.jpg"
		--> "Hello World 6.jpg"
end tell
Résultat :
"Hello World 6.jpg"

This said, moving the choose file instruction out of the tell 3Finder" block would be good practice.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 19 décembre 2019 11:37:49

Thanks for the effort guys. But I found the solution:

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

It is a piece of cake.

Renaming & avoiding the duplicate names is very essential program.

http://piyocast.com/as/archives/2818

This script avoid the duplicate name like Finder.
If default path exists, this script generate child number from 1 to 65535 (if exists).

Model: MacBook Pro 2012
AppleScript: 2.7
Browser: Safari 13.0.1
Operating System: macOS 10.14

I tested kenexzo’s script and it works as expected. An alternative that works similarly and FWIW.

set allFiles to (choose file with prompt "Choose the Files you'd like to rename:" with multiple selections allowed)
set theName to "Hello World "
set fileCounter to 1

tell application "Finder"
	repeat with thisFile in allFiles
		set theExtension to name extension of thisFile
		if theExtension is not "" then set theExtension to "." & theExtension
		if name of thisFile does not start with theName then
			repeat 100 times # 100 is an arbitrary limit to prevent endless loop on error
				set newName to theName & fileCounter & theExtension
				try
					set name of thisFile to newName
					set fileCounter to fileCounter + 1 # this line added in revision
					exit repeat
				on error
					set fileCounter to fileCounter + 1
				end try
			end repeat
		end if
	end repeat
end tell

No offense intended, maybe cleaner with a simple test replacing a try - on error - end try structure.

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

# the "increment suffix number until the name is unique" part
set counter to 1 -- starting suffix # MOVED according to Peavine proposal

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
			
			repeat 100 times
				set newName to theName & counter & theExtension
				if (name of items of theFolder) does not contain newName then exit repeat
				set counter to counter + 1
			end repeat
			
			set name of this_file to newName
		end if
	end repeat
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 20 décembre 2019 18:32:50

Thanks to insomnia and Peavine suggestion, I moved the initialization of the counter.

Yvan. Thanks for the response. I’m never offended by a different way to write one of my scripts because that’s how I learn.

One question. I intentionally placed the initialization of the counter outside the repeat loop in order to avoid rechecking for existing “Hello World” files. For example, given the following files:

Hello World 1
Hello World 2
Hello World 3
New File 1 # which will be renamed
New File 2 # which will be renamed

After renaming New File 1 to Hello World 4, there would seem to be no need to reset the counter to 1 and to loop through those files again.

BTW, my script was missing an instance where the counter should have been incremented and I’ve changed that.

@Peavine

Thanks to insomnia and your suggestion, I moved the initialization of the counter.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 21 décembre 2019 02:34:42