2015-10-19 99 views
1

我希望我的脚本来遍历邮箱的树和删除空邮箱:如何删除邮件的邮箱使用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项”归档“。”从邮箱的每个邮箱的第1项的每个邮箱的项目1号-1728“归档”

回答

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