【问题标题】:AppleScript - Copy All files/folders from source folder to destination folderAppleScript - 将所有文件/文件夹从源文件夹复制到目标文件夹
【发布时间】:2020-07-24 04:40:42
【问题描述】:

我正在尝试编写一个 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 类型循环,但在这里无法获得正确的语法。很多年没有写过 AppleScript,所以试着记住它是如何工作的。

【问题讨论】:

    标签: applescript


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2016-03-03
      相关资源
      最近更新 更多