Sunday, October 13, 2019

#1 2019-07-11 01:08:29 am

skeates
Member
Registered: 2008-11-19
Posts: 38

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

Applescript:

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.

Offline

 

#2 2019-07-11 09:19:14 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11612
Website

Re: Duplicating folder

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.

Applescript:

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


regards

Stefan

Offline

 

#3 2019-07-11 09:53:35 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 177

Re: Duplicating folder

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

Applescript:

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.

Last edited by peavine (2019-07-11 12:53:55 pm)


2018 Mac mini - macOS Mojave

Offline

 

#4 2019-07-12 02:38:36 am

skeates
Member
Registered: 2008-11-19
Posts: 38

Re: Duplicating folder

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.

Offline

 

#5 2019-07-12 10:08:28 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 177

Re: Duplicating folder

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.

Applescript:

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


2018 Mac mini - macOS Mojave

Offline

 

#6 2019-07-12 11:57:22 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 97

Re: Duplicating folder

peavine wrote:

Applescript:

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:

Applescript:

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.

Last edited by CK (2019-07-12 11:58:00 pm)

Offline

 

#7 2019-07-13 07:57:09 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 177

Re: Duplicating folder

CK wrote:

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

Applescript:

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.



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:

Applescript:

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:

rsync: mkdir "/missing/folder" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52.200.1/rsync/main.c(545) [receiver=2.6.9]
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52.200.1/rsync/io.c(453) [sender=2.6.9]

Last edited by peavine (2019-07-13 09:52:19 am)


2018 Mac mini - macOS Mojave

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)