0

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
0

2 Answers 2

0

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
1
  • red_manace I cannot thank you enough. I have to apologize since I am new to Applescript (started one month ago) and I did not know that Hazel does not accept handlers. If I did, I would have mentioned this in the first post I made about this issue. The script works flawlessly in the application now.
    – David
    Commented Dec 19, 2019 at 11:30
-1

One way would be to check for the existence of the files before you attempt the rename. Write a loop. Start where your default start file name would be. Run the names until find an untaken name.

It's harder than it should be to find out if a file exists or does not exist. This routine while long has worked for me. To avoid this handler, copy the code into your check loop.:

-- ------------------------------------------------------  
(*
  ideas from:
  https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence


 use the alias way.  Others ways ran into trouble eventually. 
*)
on fileExists(theFile) -- (String) as Boolean
    (* "System Events" and "Finder" checking for file existance revealed problems. l*)
    set debugging to false
    if debugging then log "  fileExists: theFile is " & theFile
    try
        set theAlias to theFile as alias
        set theExistance to true
    on error errMsg number n
        if debugging then log "  fileExists: n is " & n
        -- File or folder doesn't exist. 
        if n is not -43 then
            set commonError to "on error the errMsg is " & errMsg & " number is " & n
            if debugging then log "  fileExists: " & commonError
            display dialog commonError giving up after 10
            -- cause grief above.
            error "Failure of alias." number -1
        else
            set theExistance to false
        end if
    end try
    if debugging then log "  fileExists: theExistance is " & theExistance
    return theExistance
end fileExists
3
  • The OP states they cannot use code that contains a handler: "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." Commented Dec 19, 2019 at 2:42
  • Well, it's right easy to copy the code into the check loop. Commented Dec 19, 2019 at 2:52
  • Thanks for the effort guys. But I got it working with red_menace's approach above.
    – David
    Commented Dec 19, 2019 at 11:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .