感谢大家迄今为止的帮助。很抱歉,我在上一个问题的"答案"部分问了这个问题,我现在明白了我不应该这么做。。。。。所以我在这里开始了一个新的问题。
所以,我想写一个脚本,在附件到达时将其保存在电子邮件中——每个电子邮件发件人都有一个不同的文件夹。我从这个网站上的人那里得到了很多帮助。
它有点管用。。。。。对于新收到的电子邮件,它非常有效,但当我对邮箱中的旧电子邮件运行它时,它会保存一些附件,而不会保存其他附件。
我认为问题是在查找重复文件时出错(我认为这不太可能,因为我已经在文件名中添加了时间戳和电子邮件的数据戳。(所以我添加了delFile删除过程,以检查是否有相同名称的文件,以及是否找到它来删除它。
当我执行脚本时,它处理的附件比以前多了一些,但无论如何都不是全部。。。。。有趣的是,垃圾桶里什么也没放。
我现在被难住了!!作为AppleScript的新手,我还不知道如何调试或处理错误。
有人能帮忙吗?
use scripting additions
using terms from application "Mail"
on perform mail action with messages messageList for rule aRule
set destinationPath to (POSIX file "/volumes/Data/Dropbox/WORK ITEMS/Email Attachments/") as string
tell application "Mail"
repeat with aMessage in messageList
repeat with anAttachment in mail attachments of aMessage
set senderName to (extract name from sender of aMessage)
set {year:y, month:m, day:d, hours:h, minutes:min} to date received of aMessage
set timeStamp to (d & "/" & (m as integer) & "/" & y & " " & h & "." & min) as string
set attachmentName to timeStamp & " - " & name of anAttachment
set doSave to true
set originalName to name of anAttachment
if originalName contains "jpg" then
set doSave to false
else if originalName contains "jpeg" then
set doSave to false
else if originalName contains "gif" then
set doSave to false
else if originalName contains "png" then
set doSave to false
else if originalName contains "html" then
set doSave to false
else if originalName contains "ics" then
set doSave to false
end if
if doSave is true then
tell application "System Events"
if not (exists folder (destinationPath & senderName)) then
make new folder at end of alias destinationPath with properties {name:senderName}
end if
end tell
end if
if doSave is true then
set delFile to destinationPath & senderName & ":" & attachmentName
tell application "System Events" to if (exists file delFile) then delete file delFile
end if
if doSave is true then save anAttachment in file (destinationPath & senderName & ":" & attachmentName) with replacing
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from
感谢
我不确定我是否能帮助您找出脚本失败的确切原因,但我可能能帮助您了解它的失败之处。
首先,我将替换一个文件扩展名列表,您希望为long-if…else-if块排除这些扩展名。类似于:
set ignore list to {".jpg", ".jpeg", ".gif", ".png", ".html", ".ics"}
在脚本的顶部,set fileExtension to rich text (offset of "." in originalName) thru end of originalName
在循环中。
然后您可以测试:
if fileExtension is not in ignoreList then
并将其封装在保存的代码中(不需要多次执行相同的测试(。
我认为您的删除文件块是多余的,因为它应该与下面的save...with replacing
(如果文件已经存在(执行相同的操作。(如果文件存在,您可能需要删除该文件,在这种情况下,稍后删除with replacing
。(
要开始调试,首先删除处理传入消息的代码,并将其替换为set messageList to selection
。试着在你不确定发生了什么的地方插入一些display dialog <varname>
。例如,你知道附件是什么,但你确定destinationPath & senderName & ":" & attachmentName
是什么吗?
最后,请注意,我还没有在你的数据上运行这个,所以一定要备份。它不应该破坏任何东西,但安全总比抱歉好!
有任何问题请回来。祝你好运
编辑:
我在顶部添加了一个函数(on getExtension(fileName)
块。它由set fileExtension to my getExtension(originalName)
行调用
这是为了通过反转名称字符串来优化扩展获取,以便只使用第一个"."已找到。一旦获得延期,延期将被撤销。
另一个关键部分是它包含CCD_ 11。这就是AppleScript处理错误的方式。如果没有"/",则会引发错误。这被返回"跳过"的on error
捕获。(在这一点上,这并没有在主程序中使用,但可以用来将所有输出集中到一个catchall文件夹中。(
最后的变化是,我已经将保存部分封装在If originalName does not contain "/" then ... end if
中。这是为了捕获那些包含"/"的文件,并在不做任何事情的情况下"跳过"它们。
我不需要添加delay
,所以尝试不添加。这可能是在转移注意力!
set ignoreList to {".jpg", ".jpeg", ".gif", ".png", ".html", ".ics"}
set destinationPath to (POSIX file "/volumes/Data/Dropbox/WORK ITEMS/Email Attachments/") as string
on getExtension(fileName)
try
set fileName to (reverse of every character of fileName) as string
set extension to text 1 thru (offset of "." in fileName) of fileName
set extension to (reverse of every character of extension) as string
return extension
on error
return "skip"
end try
end getExtension
tell application "Mail"
set messageList to selection
repeat with aMessage in messageList
repeat with anAttachment in mail attachments of aMessage
set senderName to (extract name from sender of aMessage)
set {year:y, month:m, day:d, hours:h, minutes:min} to date received of aMessage
set timeStamp to (d & "/" & (m as integer) & "/" & y & " " & h & "." & min) as string
set attachmentName to timeStamp & " - " & name of anAttachment
set originalName to name of anAttachment
if originalName does not contain "/" then
set fileExtension to my getExtension(originalName)
if fileExtension is not in ignoreList then
tell application "System Events"
if not (exists folder (destinationPath & senderName)) then
make new folder at end of alias destinationPath with properties {name:senderName}
end if
end tell
save anAttachment in file (destinationPath & senderName & ":" & attachmentName) with replacing
end if
end if
end repeat
end repeat
end tell
对于从邮件规则调用:
use scripting additions
set ignoreList to {".jpg", ".jpeg", ".gif", ".png", ".html", ".ics"}
set destinationPath to (POSIX file "/Users/bernardharte/test/") as string
on getExtension(fileName)
try
set fileName to (reverse of every character of fileName) as string
set extension to text 1 thru (offset of "." in fileName) of fileName
set extension to (reverse of every character of extension) as string
return extension
on error
return "skip"
end try
end getExtension
using terms from application "Mail"
on perform mail action with messages messageList for rule aRule
tell application "Mail"
repeat with aMessage in messageList
repeat with anAttachment in mail attachments of aMessage
set senderName to (extract name from sender of aMessage)
set {year:y, month:m, day:d, hours:h, minutes:min} to date received of aMessage
set timeStamp to (d & "/" & (m as integer) & "/" & y & " " & h & "." & min) as string
set attachmentName to timeStamp & " - " & name of anAttachment
set originalName to name of anAttachment
if originalName does not contain "/" then
set fileExtension to my getExtension(originalName)
if fileExtension is not in ignoreList then
tell application "System Events"
if not (exists folder (destinationPath & senderName)) then
make new folder at end of alias destinationPath with properties {name:senderName}
end if
end tell
save anAttachment in file (destinationPath & senderName & ":" & attachmentName) with replacing
end if
end if
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from