Phil,
I have had mixed success this morning. I can successfully read the elements from a CSV, construct the source and destination paths, and copy to a new folder hierarchy correctly. On a mounted USB stick.
For some reason, when I have the same source structure on my mounted Synology NAS mount point, the copy process is blocked due to permissions (even with administrator privileges). In the Terminal, I can see that the destination folder structure is created on the NAS, but it does not appear in the Finder view of the mount point — even after unmounting and remounting it. Chflags hidden is off. Can't explain it after fighting this issue all morning.
Here is the CSV that I tested, and how the input and output on the USB stick appears:
And here is the AppleScript:
-- read a csv into a list of rows. Loop through the rows to concatenate first two
-- fields, and then hopefully copy the file to new destination hierarchy built from the
-- third CSV field.
property server_path : "/Volumes/Online_DAM/"
property new_path : server_path & "NewFS/"
-- property server_path : "/Volumes/A_Utility/ASTest"
-- property new_path : "/Volumes/A_Utility/VikingPath/"
property delim : ","
property isDesktop : (path to desktop as text) as alias
property csv : {"public.comma-separated-values-text", "public.delimited-values-text"}
set msg to "Choose the input CSV file:"
set rowItems to {}
-- prompt for CSV file and read its contents by row into a list
set csvfile to (choose filewith promptmsgof typecsvdefault locationisDesktop without invisibles, multiple selections allowed and showing package contents)
set csvdata to (read csvfile as «class utf8» using delimiter linefeed) as list
repeat with aRow in csvdata
-- get a list of this CSV row items split on delimiter
set rowItems to my parseCSV(aRow, delim)
-- sPath becomes the source path and filename
set sPath to server_path & first item of rowItems & middle item of rowItems
-- make any uppercase words into titlecase in destination path
set x to do shell script "ruby -e 'puts ARGV.first.gsub(/\\w+/, &:capitalize);' " & last item of rowItems
-- nPath becomes the new_path and appended destination folder
set nPath to new_path & x
-- make the folder path hierarchy if it doesn't exist, otherwise do nothing
do shell script "mkdir -p " & nPathpassword "xxxxxx" with administrator privileges
try
-- ditto will copy all existing file attributes to the destination
do shell script "ditto " & sPath & space & nPathpassword "xxxxxx" with administrator privileges
on error errmsg number errnbr
my error_handler(errnbr, errmsg)
return
end try
end repeat
return
on parseCSV(rowText, adelim)
-- split the supplied CSV row on its delimiters. Return quoted list elements.
set {TID, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, adelim}
set rowItems to text items of rowText
set AppleScript'stext item delimiters to TID
return rowItems as list
end parseCSV
on error_handler(nbr, msg)
return display alert "[ " & nbr & " ] " & msg as critical giving up after 10
end error_handler