Similar to the problem described here, I have a text file with filenames, and a folder with image files that need to be copied to another destination. As these are hundreds of files quite frequently, it would help a lot to automate this task.
The challenge is that some files in the text file might not exist, and the current version of the script I have can not account for this case and throws an error.
In summary, I would like to use Automator & Apple Script to:
Here's what I have so far - this works as long as there is an actual image for every line in the text file:
And AppleScript:
on run {input, parameters}
set imgDestination to input's item 2
set imgSource to input's item 1
set imgNameFile to choose file with prompt {"Select file of image filenames:"}
set imageList to every paragraph of (read imgNameFile)
repeat with eachName in imageList
tell application "Finder"
set targetImageFile to item 1 of (get every file in folder imgSource whose name = (eachName as text))
duplicate targetImageFile to folder imgDestination
end tell
end repeat
return input
end run
Unfortunately my skills are not enough to come up with a if/else scenario for 5 & 6, so any help would be much appreciated.
You have to check if the file exists.
Your script is quite inefficient because a whose
clause in the Finder is very expensive although you could avoid the loop with
tell application "Finder"
duplicate (get every file in folder imgSource whose name is in imageList) to folder imgDestination
end tell
A more efficient way is to retrieve the file names once and check if the list contains the current name.
There could be an unexpected behavior if the text file is UTF8 encoded which is a quasi standard. If so you have to read
the text file as «class utf8»
.
Finally if the source and destination references in input
are AppleScript alias specifiers you have to remove all occurrences of the folder
keyword.
on run {input, parameters}
set imgDestination to input's item 2
set imgSource to input's item 1
set imgNameFile to choose file with prompt {"Select file of image filenames:"}
set imageList to every paragraph of (read imgNameFile as «class utf8»)
tell application "Finder" to set fileNames to name of every file in folder imgSource
repeat with eachName in imageList
if fileNames contains eachName then
tell application "Finder" to duplicate file eachName of folder imgSource to folder imgDestination
end if
end repeat
return input
end run