2015-10-19 98 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