I have a script that occasionally has to make duplicate files in a folder.
If the file original file name is A_File_Name.psd then the first duplicate will be named A_File_Name_1.psd, the next one A_File_Name_2.psd etc.
This process means that I have to get the Finder to duplicate the file - giving me a file called A_File_Name copy.psd, and then grab the returned reference and change the name.
This script has to deal with file servers where the old 31 character file name limit is still enforced, which means that the Finder’s desire to put " copy" into the name can make the name too long, with an error resulting.
At the moment, I deal with this by trapping the error and telling the user to shorten the name and start again.
The Finder’s scripting dictionary doesn’t seem to offer a way of specifying a file name when a duplicate is made even though you can specify a different location.
It’s annoying because the file name I ultimately want will come in under the 31 character limit.
Does any one know a way to have the Finder change a file’s name during duplication?
I think you may be stuck with a workaround here, Simon.
If the file is duplicated to another location, the word “copy” shouldn’t be appended - so you could:
duplicate the file to a separate location
change the duplicate’s name as required
move the duplicate back to the original folder.
Alternatively, to avoid using another location, you might:
shorten the file’s name (to a temporary substitute)
duplicate the file to the same folder
change the duplicate’s name as required
restore the original file’s name.
set f to choose file with prompt "Choose a file to duplicate:"
tell application "Finder"
set {name:fName, name extension:fExtn, folder:fFldr} to f
set baseName to fName's text 1 thru -((count fExtn) + 2) & "_"
set n to 1
set newName to baseName & n & "." & fExtn
considering case
tell (get name of fFldr's files) to repeat while newName is in it
set n to n + 1
set newName to baseName & n & "." & fExtn
end repeat
end considering
set f's name to "*temp*"
set (duplicate f)'s name to newName
set f's name to fName
end tell