0

我一直在尝试让系统事件在 AppleScript 中复制文件,但我一直失败:) 我最终总是收到错误“错误“文件无法复制。”编号 -1717”。所以我改变了策略并尝试使用 Finder 来确保我尝试做的事情是正确的。这是有效的代码:

告诉应用程序“系统事件”

set desktopFolder to (path to desktop folder) as string
set fullPath to desktopFolder & "Temp Export From DO"

set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
    set source to path of DOEntry
    log "Source file: " & source
    set destination to fullPath as string
    log "Destination folder: " & destination
    tell application "Finder"
        duplicate file source to folder destination with replacing
    end tell
end repeat

结束告诉

如果我删除最后一个通知,以便它使用系统事件,我会得到上面提到的相同错误。系统事件标准套件的字典有一个“重复”命令,所以我不确定这里发生了什么。此外,APress 中的“Learning AppleScript,第 3 版”指出:

“系统事件中一个特别烦人的遗漏是它还不能复制文件和文件夹;如果你需要这样做,Finder 是你最好的选择。”

第 3 版是从 2010 年开始的。似乎即使在 Mountain Lion 中这仍然是正确的。谁能证实这一点?1717 错误号将其他任何地方列为处理程序错误,我没有使用处理程序。

4

2 回答 2

1

不幸的是,您不能使用系统事件复制文件 - 您必须使用 Finder。即使在 adayzdone 提供的答案中,系统事件实际上也没有处理重复。

这看起来像是在工作(因为它在系统事件告诉块内)......

tell application "System Events"
    duplicate myFile to myFolder
end tell

...但是如果您检查事件日志,您会看到 Finder 实际上正在执行复制。在幕后,您将两个 Finder 对象传递给系统事件。系统事件不知道如何处理 Finder 对象,因此将执行传递给对象的所有者 Finder,后者执行命令。

对于 AppleScript 中的文件复制,不幸的是,您只能使用 Finder 或通过do shell script.

于 2014-09-18T04:08:43.357 回答
0

尝试:

tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
    log "Source file: " & DOEntry
    log "Destination folder: " & desktopFolder
    tell application "Finder" to duplicate file DOEntry to desktopFolder with replacing
end repeat

如果您不需要记录值,您可以简单地:

tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
tell application "Finder" to duplicate theDOEntries to desktopFolder with replacing

或者:

set desktopFolder to quoted form of (POSIX path of (path to desktop folder as text) & "Temp Export From DO")
do shell script "find '/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries' -name \"*.doentry\" -type f -print0 | xargs -0 -I {} cp -a {} " & desktopFolder

回到您的问题,重复的命令会创建 Finder 项目的重复项。您可以使用系统事件来复制 Finder 项目,如下所示:

tell application "Finder"
    set myFile to file ((path to desktop as text) & "Test File.txt")
    set myFolder to folder ((path to desktop as text) & "Test Folder")
end tell

tell application "System Events"
    duplicate myFile to myFolder
end tell
于 2012-11-07T13:41:51.597 回答