In the Finder, I’ve selected a folder which contains a single file named mac1024.png. I want to make a copy of that file with the name icon_512x512@2x.png and then rename the original to icon_1024.png.
This script works. Before running it, I make a copy of the folder and then run the script on the copy, because I’m not a maniac:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
set theseItems to selection
set thisFolder to item 1 of theseItems
set folderFiles to every file of thisFolder
repeat with thisFile in folderFiles
set oldFileName to name of thisFile
set newFileName to "icon_1024x1024.png"
set new2xFileName to "icon_512x512@2x.png"
set new2xFile to (duplicate thisFile to thisFolder)
set name of new2xFile to new2xFileName
set name of thisFile to newFileName
end repeat
end tell
I have another version that I can run on the original folder because it makes a copy of it for me and then operates on the copy.
Only, it doesn’t work. The line that duplicates the PNG file fails with “An item with the same name already exists in this location. (-15267).” The only difference in the code is the three lines after the comment:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
set theseItems to selection
set thisFolder to item 1 of theseItems
-- Duplicating the folder here causes an error
-- when we try to duplicate the file later. 🤨
set parentFolder to container of thisFolder
set newFolder to (duplicate thisFolder to parentFolder)
set folderFiles to every file of newFolder
repeat with thisFile in folderFiles
set oldFileName to name of thisFile
set newFileName to "icon_1024x1024.png"
set new2xFileName to "icon_512x512@2x.png"
set new2xFile to (duplicate thisFile to thisFolder)
--> An item with the same name already exists in this location. (-15267)
set name of new2xFile to new2xFileName
set name of thisFile to newFileName
end repeat
end tell
I can’t figure out why Finder can duplicate the file as mac1024 2.png in the first version but forgets to increment the filename in the second version. Anyone?