Le 9 oct. 2006 à 18:27, Christopher Nebel a écrit :
On Oct 9, 2006, at 7:29 AM, Yvan KOENIG wrote:
Best as I can tell, you can't do it directly. Here is my work around.
set file_to_copy to (choose file)
set target_folder to (choose folder)
tell application "Finder"
try
duplicate file_to_copy to target_folder without replacing
on error msg number num
if num is -15267 then
set new_name to display dialog msg & " Please choose another name for the copy." default answer (name of file_to_copy) & " copy"
set new_file to duplicate file_to_copy
move new_file to target_folder
else
display dialog msg
end if
end try
end tell
and what if (name of file_to_copy) & " copy" already exists ? ;-)
It's irrelevant, because that block will never be executed. The Finder's "duplicate" command (without replacing) will rename the new file as necessary ("foo copy n") to avoid a collision. ("How do I get a reference to the new file if I don't know its name?", you ask? "Duplicate" returns a reference to the new file as its result.)
What is irrelevant ?
Look at this log report:
tell current application
choose file
alias "Macintosh HD:Users:yvankoenig:Desktop:dossier 1:Modernise_AW v1.applescript"
choose folder
alias "Macintosh HD:Users:yvankoenig:Desktop:dossier 2:"
end tell
tell application "Finder"
get name of alias "Macintosh HD:Users:yvankoenig:Desktop:dossier 1:Modernise_AW v1.applescript"
"Modernise_AW v1.applescript"
display dialog "Un élément du même nom existe déjà à l’emplacement de destination. Please choose another name for the copy." default answer "Modernise_AW v1.applescript copy"
{text returned:"Modernise_AW v1.applescript copy", button returned:"OK"}
duplicate alias "Macintosh HD:Users:yvankoenig:Desktop:dossier 1:Modernise_AW v1.applescript"
document file "Modernise_AW v1 - copie.applescript" of folder "dossier 1" of folder "Desktop" of folder "yvankoenig" of folder "Users" of startup disk
move document file "Modernise_AW v1 - copie.applescript" of folder "dossier 1" of folder "Desktop" of folder "yvankoenig" of folder "Users" of startup disk to alias "Macintosh HD:Users:yvankoenig:Desktop:dossier 2:"
current application
"Erreur dans Finder : Un élément du même nom existe déjà à l’emplacement de destination."
Sure, the entered new name is not used.
It's a good thing because the word "copy" was added "after" the name extension ;-)
But the script creates a file named "Modernise_AW v1 - copie.applescript" in the source folder
and fails when such a file already exists in the destination one!
Yvan KOENIG