AppleScript - 将所有 files/folders 从源文件夹复制到目标文件夹

AppleScript - Copy All files/folders from source folder to destination folder

我正在尝试编写一个 AppleScript,它将内容(文件夹和文件)从指定的源文件夹简单地复制到指定的目标文件夹。目前我的脚本运行但只复制一个文件,我不知道如何让它复制文件夹中的所有文件。

这是我的脚本:

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

我假设我需要包含一个 for each 类型循环,但无法在此处获得正确的语法。很多年没有写过 AppleScripts,所以试着记住它是如何工作的。

如果目标 "Project1" 文件夹中没有内容,那么复制文件夹可能会更快:

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

但是,如果那不是一个选项,那么我会坚持使用您的方法并改为复制文件夹的内容:

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

请记住,如果任何目标中有一个文件或文件夹打算被传入的源项目之一占用,Finder 将引发错误。您通常会在复制之前加入某种检查。