AppleScript自动保存邮件附件-适用于新邮件,但不适用于旧邮件

AppleScript自动保存邮件附件-适用于新邮件,但不适用于旧邮件,applescript,attachment,Applescript,Attachment,谢谢大家迄今为止的帮助。抱歉,我在上一个问题的“答案”部分问了这个问题,我现在明白我不应该这么做。。。。。所以我在这里开始了一个新问题 因此,我想编写一个脚本,在附件到达时将其保存在电子邮件中-每个电子邮件发件人有一个不同的文件夹。我从这个网站上得到了很多帮助 这有点奏效。。。。。对于新收到的电子邮件,它工作得很好,但当我在邮箱中对我的旧电子邮件运行它时,它会保存一些附件,而不是其他附件 我认为问题在于查找重复文件时出错(我认为这不太可能,因为我已将时间戳与电子邮件的数据戳一起添加到文件名中)。

谢谢大家迄今为止的帮助。抱歉,我在上一个问题的“答案”部分问了这个问题,我现在明白我不应该这么做。。。。。所以我在这里开始了一个新问题

因此,我想编写一个脚本,在附件到达时将其保存在电子邮件中-每个电子邮件发件人有一个不同的文件夹。我从这个网站上得到了很多帮助

这有点奏效。。。。。对于新收到的电子邮件,它工作得很好,但当我在邮箱中对我的旧电子邮件运行它时,它会保存一些附件,而不是其他附件

我认为问题在于查找重复文件时出错(我认为这不太可能,因为我已将时间戳与电子邮件的数据戳一起添加到文件名中)。因此,我添加了delFile delete进程,以检查是否存在同名文件,如果发现该文件,则将其删除

当我执行脚本时,它处理的附件比以前多了一些,但不是全部。。。。。有趣的是,没有任何东西被放进垃圾箱

我现在被难住了!!作为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

谢谢

我不确定我能帮你解释脚本失败的确切原因,但我也许能帮你看看它失败的地方

首先,我将替换一个文件扩展名列表,您希望将其排除在长if…else if块之外。比如:

将忽略列表设置为脚本顶部的{.jpg'、“.jpeg”、“.gif”、“.png”、“.html”、“.ics”}
,并在循环中将文件扩展名设置为富文本(偏移量为“.”in originalName)

然后,您可以测试:

如果文件扩展名不在ignoreList中,则

并将其包装在保存代码中(不需要多次执行相同的测试)

我认为您的delete file块是多余的,因为它应该与下面的
save…相同,替换
(如果文件已经存在)。(如果文件存在,您可能希望删除该文件,在这种情况下,请稍后通过替换删除
。)

要开始调试,首先删除处理传入消息的代码,并将其替换为
set messageList To selection
。尝试在不确定发生了什么的地方插入一些显示对话框。例如,您知道什么是anAttachment,但您确定
destinationPath&senderName&“:”&attachmentName
是什么吗

最后,请注意,我没有对您的数据运行此操作,因此请务必进行备份。它不应该破坏任何东西,但安全总比后悔好

请带着任何问题回来。祝你好运

编辑:

我在顶部添加了一个函数(getExtension(fileName)
块上的
。这是由行
set fileExtension to my getExtension(originalName)

这是通过反转名称字符串来优化扩展名获取,以便只找到第一个“.”。一旦获得扩展名,将反转扩展名

另一个关键部分是,它包含
try…on error…end try
。这是AppleScript处理错误的方式。如果没有“/”错误,则抛出错误。这由
on error
捕获,返回“skip”。(此时,它不用于主程序,但可用于将所有输出导入catchall文件夹。)

最后一个更改是,我将保存部分包装在
中,如果originalName不包含“/”则…如果
结束。这是为了捕获包含“/”的文件,并在不做任何操作的情况下“跳过”它们

我不需要添加
延迟
,所以请尝试在没有延迟的情况下开始。这可能是一种危险的做法

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

另一个想法是:
destinationPath
正确吗?它不应该以大写字母“V”开头吗?谢谢你的上述内容,对解释和代码都很有帮助。所以……很有趣!我像上面一样运行它,带有4封电子邮件的显示对话,运行得很好。我在邮件命令中添加了回去,运行了4封电子邮件,运行得很好,然后我删除了它编辑了显示对话框并运行了200封电子邮件,这几乎不起作用,但当我将显示对话框命令放回时,它工作正常。是否有可能它运行得太快,邮件无法继续发送邮件消息(与中的显示对话框的唯一区别是它运行缓慢,因为我需要确认我已看到邮件)我将编辑原始答案中的代码,并解释我所做的工作。这就解释了问题产生的原因:冒号在macOS上用作路径分隔符。老实说,我不确定它是如何从一个位置转换到另一个位置的,但您可以通过在名称字符串中的该位置替换其他位置来处理它。可能下次;)我无法确定它不作为邮件规则运行的原因。我怀疑这与权限/沙箱有关。我建议你发布另一个关于这方面的问题-以及你的脚本。这样你会得到更多帮助。在你最初的问题中,我建议你去掉
if
else if
并使用一个列表,e我还建议你不要在日期中使用
/
,而是使用
-
。你应该听那些比你更了解这一点的人说!