Duplicate and rename one file with a specified filename

I’m looking for simple code to duplicate and rename one file with a specific filename. The file to duplicate is “/Applications/Endive Clients/GenericEndive.app”. The new filename is “/Applications/Endive Clients/EndiveName.app”

You may use :

use AppleScript version "2.4"
use framework "Foundation"
use script "FileManagerLib" version "2.0.2" # available at : <www.macosxautomation.com/applescript/apps/>
use scripting additions


set sourceFile to "/Applications/Endive Clients/GenericEndive.app"
set destFolder to "/Applications/Endive Clients/"
set destName to "EndiveName.app"

copy object sourceFile to folder destFolder new name destName with replacing and return path

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 29 aout 2019 21:54:23

Another way to do this.


set sourcefile to "/Applications/Endive Clients/GenericEndive.appp/"
set destinationFile to "/Applications/Endive Clients/EndiveName.app/"

try
	do shell script "ditto " & quoted form of sourcefile & space & quoted form of destinationFile
on error errorMessage
	display dialog "The following error message was reported:" & return & return & errorMessage ¬
		buttons {"OK"} cancel button 1 default button 1
end try

BTW, this script overwrites an existing file without prompting and creates the target directory if it is changed to something other than the source directory and doesn’t exist.


set sourceFilePath to "/Applications/Endive Clients/GenericEndive.app"

tell application "Finder"
	set destinationFile to duplicate file sourceFilePath
	set name of destinationFile to "EndiveName.app"
end tell

I apologizes but Finder is unable to work with POSIX paths.

If we replace the first instruction by
[format]set sourceFilePath to POSIX file “/Applications/Endive Clients/GenericEndive.app”[/format]
which is supposed to build a descriptor usable by the Finder, it works as long as we don’t call it twice. In the late case we will get three items in the folder :
[format]EndiveName.app
GenericEndive copie.app
GenericEndive.app[/format]

This is why my proposal carefully stated : with replacing

Here the script will never create the target directory because this one is the source folder itself.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 30 aout 2019 09:13:23

You are right. I tested my first script with choose file (which gives alias). The correct script is:


set sourceFile to POSIX file "/users/123/Desktop/GenericEndive.app"

tell application "Finder"
	set destinationFile to duplicate file sourceFile
	set name of destinationFile to "EndiveName.app"
end tell

This creates a duplicate and renames it. I do not find any third file.

@KniazidisR

I carefully wrote:
it works as long as we don’t call it twice. In the late case we will get three items in the folder
but it seems that you missed that.

Try to run it twice and you will get the described problem.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 30 aout 2019 15:37:30

OK, I ran the script twice and got this error: “The operation can’t be completed because there is already an item with that name.” In that case the script fails, because can’t rename the copy file. And, in that case doesn’t created 3 files. The 3-rd file (“/Applications/Endive Clients/”) is a file, already created from the first execution.
But why I should run the script twice? To get the error? It is unclear.

And, if you want the script with a guarantee of replacing, and run it multiple times, then run this:


set sourceFile to POSIX file "/users/123/Desktop/GenericEndive.app"

tell application "Finder"
	set destinationFile to duplicate file sourceFile
	try
		delete application file "EndiveName.app" of (container of destinationFile)
	end try
	set name of destinationFile to "EndiveName.app"
end tell

The OP hasn’t said what he wants to happen if the target file already exists. Like, Yvan, I assumed he wants to overwrite an existing file but that’s just a guess.

Thank you for the suggestions. KniazidisR’s solution is what I used and it works great!

FYI the duplicated files will always be new files; even when i run the script multiple times. My main code deletes the new file at the end of each loop of the main code.

Thanks!