From my point of view, some instructions are wrongly written.
(1) set files_source to (choose folder with prompt “Choose the source folder”) as text
What need to coerce the alias item returned by choose folder into text to call it later as a folder ?
Keet the alias as is and speak to it directly below.
(2) set files_dest to (choose folder with prompt “Choose your destination folder for the copied files.”) as alias
What need to coerce into alias an item which is already an alias ?
set file_ref to (choose file with prompt “Select a txt file to read:” default location files_dest)
(3) set files_list to paragraphs of (read file_ref as «class utf8»)
I don’t know your text file so maybe using «class utf8» is right for it, here it was useless.
tell application “Finder”
(4) set theFiles to a reference to entire contents of folder files_source
According to what I wrote above (1), if you leave files_source of class alias, here you may code :
set theFiles to a reference to entire contents of files_source
repeat with thisItem in theFiles
set theFileName to name of thisItem as text
if theFileName is not in files_list then
try
– or move thisItem to files_dest if you want to move it
duplicate thisItem to files_dest
on error
display dialog "Duplicate files named " & theFileName & “. Overwrite or skip?” buttons {“Cancel”, “Overwrite”, “Skip”} with icon caution
if button returned of the result is “Overwrite” then
duplicate thisItem to files_dest with replacing
end if
end try
end if
end repeat
end tell
This said, this first part of your script worked flawlessly here.
Now it’s time to speak of the second part.
set files_source to (choose folder with prompt “Choose the source folder”) as text
set files_dest to (choose folder with prompt “Choose your destination folder for the copied files.”) as alias
set file_ref to (choose file with prompt “Select a txt file to read:” default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
repeat with aParagraph in files_list
try
(5) do shell script "cp " & quoted form of POSIX path of files_source & quoted form of aParagraph & space & quoted form of POSIX path of files_dest
I’m not sure that it’s wrong but I don’t like to concatenate quoted strings. I think that it is neater to quote after concatenation.
do shell script "cp " & quoted form of( POSIX path of files_source & aParagraph) & space & quoted form of POSIX path of files_dest
end try
end repeat
Here, running :
set files_source to (choose folder with prompt "Choose the source folder") as text
set files_dest to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set file_ref to (choose file with prompt "Select a txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
repeat with aParagraph in files_list
try
do shell script "cp " & quoted form of (POSIX path of files_source & aParagraph) & space & quoted form of POSIX path of files_dest
end try
end repeat
It copied the files listed in the text file.
But I’m not sure that the text file contain only file names. Maybe it contain folder names.
In such case the code must be edited because your original instruction refuse to treat folders.
This edited version is able to copy folders(with their contents) and here it did the job.
set files_source to (choose folder with prompt "Choose the source folder") as text
set files_dest to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set file_ref to (choose file with prompt "Select a txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
repeat with aParagraph in files_list
try
do shell script "cp -R " & quoted form of (POSIX path of files_source & aParagraph) & space & quoted form of POSIX path of files_dest
end try
end repeat
Yvan KOENIG (VALLAURIS, France) lundi 13 octobre 2014 15:01:52