【发布时间】: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