如何使用Applescript删除邮箱

时间:2015-10-19 17:35:32

标签: email applescript

我希望我的脚本遍历邮箱树并删除空邮箱:

tell application "Mail"
    my cleanup(mailbox "Archives")
end tell

on cleanup(box)
    tell application "Mail"

    if (count of mailboxes of box) > 0 then
        repeat with mbx in mailboxes of box

            my cleanup(mbx)
        end repeat
    else
        if (count of messages of box) = 0 then delete box
    end if


    end tell
end cleanup

“删除框”会导致错误: 错误“邮件出错:无法获取邮箱每个邮箱的第1项的每个邮箱的第1项”Archives \“。”从邮箱“档案馆”的每个邮箱的项目1的每个邮箱的项目1中的号码-1728

1 个答案:

答案 0 :(得分:1)

有两个问题:

•行

中的索引变量mbx
repeat with mbx in mailboxes of box

item 1 of every mailbox of box而不是mailbox "something" of box的引用。在将变量传递给具有contents of的处理程序之前,您必须取消引用该变量。

•如果例如项目1已被删除,则在同一行中您将收到错误,项目2现在是项目1,并且不再有项目2。为避免这种情况,请使用关键字get来检索在循环期间不受删除影响的复制引用。

tell application "Mail"
    my cleanup(mailbox "Archives")
end tell

on cleanup(box)
    tell application "Mail"

        if (count of mailboxes of box) > 0 then
            repeat with mbx in (get mailboxes of box)

                my cleanup(contents of mbx)
            end repeat
        else
            if (count of messages of box) = 0 then delete box
        end if

    end tell
end cleanup