0
votes

I'm trying to write an AppleScript that will simply copy the contents (both folders and files) from a specified source folder to a specified destination folder. At the moment my script runs but only copies one file and I can't work out how to get it to copy all files in the folder.

Here's my script:

set sourceFolder to (POSIX file "/Users/benny/Desktop/Projects/Source/Project1") as alias
set destinationFolder to (POSIX file "/Users/benny/Documents/Masters/Project1") as alias

tell application "System Events"
    set availableSourceFiles to every file of sourceFolder whose visible is true
    set filesOfTargetFolder to files of destinationFolder whose visible is true
end tell

-- if no more source file is available, quit this script
if (count of availableSourceFiles) = 0 then
    quit
end if

set sourceFile to (first item of availableSourceFiles) as alias
-- use the Finder to copy the file
tell application "Finder"
    -- duplicate the file to the target folder
    duplicate sourceFile to destinationFolder
end tell

I'm assuming I need to include a for each type loop but can't get the syntax correct here. Haven't written AppleScripts in many years so trying to remember how it all works.

1

1 Answers

0
votes

If the destination "Project1" folder doesn't have stuff in it already, then duplicating the folder is likely to be quicker:

tell application id "com.apple.Finder" to duplicate folder POSIX file ¬
        "/Users/benny/Desktop/Projects/Source/Project1" to the folder ¬
        POSIX file "/Users/benny/Documents/Masters" with replacing

However, if that's not an option, then I'd stick with your method and copy the contents of the folder across instead:

set here to POSIX file "/Users/benny/Desktop/Projects/Source/Project1"
set there to POSIX file "/Users/benny/Documents/Masters"

tell application id "com.apple.Finder" to duplicate ¬
             every item in the folder here to there

Bear in mind that if there's a file or folder at any of the destinations that intended to be occupied by one of the incoming source items, Finder will throw an error. You would typically incorporate some sort of check ahead of the copy.