Duplicating folder

Good day,

I’ve put the following together which for the most part works, but I need some help with a few bits

property source : "Volumes:CAD Setup:Vectorworks:2019:" as alias
property destination : "Users:Shared:Vectorworks:Workspace:" as alias

tell application "Finder"
	duplicate source to destination
end tell

Firstly off at the moment it copies all the data from the source to the destination if the destination folder exists so I need something in there to check if the destination folder does not exist then create it and copy the contents of the source into it.

Second if the folder already contains the contents from a previous copy it errors out saying: Finder got an error: An item with the same name already exists in this location.

The point of this is to have a centralise set of information that we can update and change as and when we like. Users can then periodically run the script to ensure they have the correct and most up today information from the central repository.

First of all although the syntax (meanwhile) works HFS paths should always begin with a disk name, for example CAD Setup:... rather than Volumes:CAD Setup:…

Other suggestions:

• There is a path to syntax to get the folder /Users/Shared.
• Don’t hard-code paths in properties.
• If the destination folder does not exist the code throws an error. Use string paths instead of alias specifiers…

The easiest way to check and create a folder on demand is the mkdir command of the shell and to replace the contents of the destination folder just add with replacing.

set source to "CAD Setup:Vectorworks:2019:"
set destination to (path to shared documents folder as text) & "Vectorworks:Workspace:"

do shell script "/bin/mkdir -p " & quoted form of POSIX path of destination
tell application "Finder"
	duplicate folder source to folder destination with replacing
end tell

StefanK has answered the question raised by the OP, but, FWIW, the following is an alternative to consider:

set sourceFolder to quoted form of "/Volumes/CAD Setup/Vectorworks/2019/"
set destinationFolder to quoted form of "/Users/Shared/Vectorworks/Workspace/"

try
	do shell script "rsync --archive --update " & sourceFolder & " " & destinationFolder
on error
	display dialog "The rsync utility reported an error." buttons {"OK"} cancel button 1
end try

A few comments:

  • Rsync creates the destination folder if it does not exist.

  • The script copies both files and folders in the source folder.

  • Rsync is relatively fast when a large number of files are involved.

Thanks that worked great as always you can’t go wrong on this forum. Just got back to apple script after a number of years of not touching it so relearning again.

I happened to notice that StefanK’s script copies the source folder and its contents to the destination folder, while my script copies the contents of the source folder to the destination folder. I’m not sure which the OP wants, but, FWIW, the folllowing version of my script will work as StefanK’s.

set sourceFolder to quoted form of "/Volumes/CAD Setup/Vectorworks/2019"
set destinationFolder to quoted form of "/Users/Shared/Vectorworks/Workspace/"

try
	do shell script "rsync --archive --update " & sourceFolder & " " & destinationFolder
on error
	display dialog "The rsync utility reported an error." buttons {"OK"} cancel button 1
end try

A possibly more useful error handler could report the contents of stderr:

try
	do shell script "rsync --archive --update " & sourceFolder & " " & destinationFolder
on error e
	display dialog e
end try

This is assuming rsync itself returns vaguely helpful error messages.

For consideration by the OP, it seems that the goal might better be served by an actual shell script rather than an AppleScript, (if your means of executing scripts isn’t limited to AppleScripts). It’s not a life-changing difference outwardly, but it might save a bit of effort under the hood.

Rsync error messages can be long and somewhat confusing to a user who is not familiar with this utility. If the OP wants to provide this information to the end user, an alternate approach might be to first notify the user of the error and then, optionally, to show the actual error message.

A script that demonstrates this approach is:

try
	do shell script "rsync --archive --update /Users/Shared/ /missing/folder/"
on error errorMessage
	display dialog "The rsync utility reported an error." buttons {"Cancel", "View Error Message"} ¬
		default button 2 cancel button 1 with icon stop
	display dialog errorMessage buttons {"OK"} default button 1 cancel button 1
end try

The error message returned by rsync after running the above script is: