【问题标题】:does System Events in AppleScript support duplicating files?AppleScript 中的系统事件是否支持复制文件?
【发布时间】:2012-11-07 04:26:27
【问题描述】:

我一直在尝试让系统事件在 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 错误编号将其他任何地方都列为处理程序错误,我没有使用处理程序。

【问题讨论】:

    标签: applescript


    【解决方案1】:

    很遗憾,您无法使用系统事件复制文件 - 您必须使用 Finder。即使在 adayzdone 提供的答案中,系统事件实际上也没有处理重复。

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

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

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

    对于 AppleScript 中的文件复制,很遗憾,您只能通过 do shell script 使用 Finder 或命令行。

    【讨论】:

      【解决方案2】:

      试试:

      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
      

      【讨论】:

      • 感谢您的回复,但您的代码似乎与我的代码几乎相同,但更短一些。我想我的主要问题不是如何让某些东西发挥作用……因为我注意到我的代码有效。我的问题是系统事件是否真的允许复制文件,并且根据我发布的书名和我的实验,即使“方法”存在,它也不允许。
      • 甜蜜。谢谢。看起来我在创建路径时缺少的是“文件”和“文件夹”对象。显示我对 applescript 的了解有多么少。似乎有无穷无尽的方法来做需要大量试验和错误的事情。
      猜你喜欢
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 2022-11-11
      • 1970-01-01
      • 2021-10-11
      相关资源
      最近更新 更多