【问题标题】:Need a fix for an Applescript bug in my effort to determine existence of renamed file在我努力确定重命名文件的存在时需要修复 Applescript 错误
【发布时间】:2014-10-19 15:12:27
【问题描述】:

我有一个脚本可以在重命名后将一组选定电子邮件中的所有电子邮件附件复制到一个文件夹中。因为附件有时在原处具有相同的名称,即使重命名,我需要在后续版本中添加类似“副本”的内容,这样它们就不会相互保存。有了一些编程知识但对 AppleScript 了解甚少,我拼凑起来:

告诉应用程序“邮件”

set theMessages to selection
set theOutputFolder to (choose folder) as string
repeat with a from 1 to length of theMessages
    set theMessage to item a of theMessages
    set {year:y, month:m, day:d} to date sent of theMessage
    set theDate to (y * 10000 + m * 100 + d) as string
    set theAttachments to every mail attachment of theMessage
    repeat with b from 1 to length of theAttachments
        set theAttachment to item b of theAttachments
        set theAttachmentName to theDate & " " & name of theAttachment
        set theSavePath to theOutputFolder & theAttachmentName
        tell application "System Events" to exists file theSavePath
        repeat while the result
            set oldDelims to AppleScript's text item delimiters
            set AppleScript's text item delimiters to {"."}
            set delimitedList to every text item of theSavePath
            set suffix to "." & last item of delimitedList
            try
                copy text items 1 thru -2 of theSavePath to theSavePathBase
            on error
                copy theSavePath to theSavePathBase
            end try
            -- display dialog "theSavePath pre- : " & theSavePath
            -- display dialog "theSavePathBase & ' copy' & suffix pre- " & theSavePathBase & " copy" & suffix
            set AppleScript's text item delimiters to oldDelims
            copy theSavePathBase & " copy" & suffix to theSavePath
            -- display dialog "theSavePath post- : " & theSavePath
            tell application "System Events" to exists file theSavePath -- <<< *** BOMBS HERE
            -- display dialog "Made it past existence check."
        end repeat
        try
            display dialog "preparing to save..."
            save theAttachment in theSavePath
        on error errText number errNum
            display dialog errText
        end try
    end repeat
end repeat

说完

它可以工作,只是当遇到需要修复副本名称时,它会在指定位置爆炸并显示以下消息:“系统事件出错:无法制作{”...桌面:邮件附件:20140830 Resumé 2014”、“pages”、“copy”、“.zip”}转成整数类型。”我重构名称的方式将类型更改为存在检查器无法处理的内容。

任何帮助将不胜感激。帮助解释会更好,因为我不是 AppleScripter,但我很感兴趣。谢谢!

【问题讨论】:

  • 对于初学者,您的行“告诉应用程序“系统事件”以存在文件 theSavePath”不在重复循环中,所以我认为流程不会解释文件名冲突的情况已经发生了第二次。它只会使用相同的“复制”方案。

标签: email applescript file-exists


【解决方案1】:

您的错误消息告诉您问题所在。注意错误消息周围的括号 {}。

Can’t make {"...Desktop:Mail Attachments:20140830 Resumé 2014", "pages", " copy", ".zip"} into type integer."

这表明这是一个项目列表,而不是一个字符串。因此,您需要先将其设为字符串,因此请更改...

copy text items 1 thru -2 of theSavePath to theSavePathBase

到:

copy (text items 1 thru -2 of theSavePath) as text to theSavePathBase

话虽如此,我认为您的实际复制命令(如下)不会起作用。您似乎希望复制命令重命名文件并一步复制所有文件。

copy theSavePathBase & " copy" & suffix to theSavePath

复制命令将无法找到“...Desktop:Mail Attachments:20140830 Resumé 2014.pages.copy.zip”,因为它不存在。这是重命名文件所需的名称。我认为您最好的方法是使用“cp”unix 可执行命令来执行您的副本,因为它可以一步复制和重命名。类似这样的事情,尽管您必须弄清楚如何获取变量“theAttachmentCurrentPath”的实际值。

do shell script "cp " & quoted form of POSIX path of theAttachmentCurrentPath & space & quoted form of POSIX path of (theOutputFolder & text 1 thru -5 of theAttachmentName & " copy" & text -4 thru -1 of theAttachmentName)

【讨论】:

  • 谢谢!我认为这是问题所在,但不知道如何影响类型转换。我尝试寻找解决方案,但不知何故空了。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多