如何使用 Applescript 删除邮箱

how to delete a mail mailbox with 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

"delete box" 导致错误: 错误 "Mail got an error: Can’t get item 1 of every mailbox of item 1 of every mailbox of mailbox \" 存档\"." number -1728 from item 1 of every mailbox of item 1 of every mailbox of mailbox "Archives"

有两个问题:

• 行

中的索引变量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