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

时间:2020-07-24 04:40:42

标签: applescript

我正在尝试编写一个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了,所以想记住它的工作原理。

1 个答案:

答案 0 :(得分:0)

如果目标"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 会引发错误。通常,您会在复制之前加入某种检查。