I’m wanting to have a script in my Mail.app that deletes a mailbox. For some reason, when I delete items in this app, a new mailbox appears: Recovered Messages (accountname). I want a script to delete these. They’re unnecessary and add to the lovely legacy confusion this app provides.
But I can’t seem to get the mailbox. That’s the error:
error "Mail got an error: Can’t get mailbox \"On My Mail\"." number -1728 from mailbox "On My Mail"
Here’s my code:
tell application "Mail"
my cleanup(mailbox "On My Mail")
end tell
on cleanup(box)
tell application "Mail"
if (count of mailboxes of box) > 0 then
repeat with mbx in (get mailboxes of box)
if name of box contains "Recovered Messages" then delete box
end repeat
else
-- if (count of messages of box) = 0 then delete box
end if
end tell
end cleanup
Anybody got any insight? I tried the following code which listed all the mailboxes, however the ones I’m looking for are missing: On My Mac and Recovered Messages (myaccountname).
tell application "Mail"
set myboxes to every mailbox
end tell
The kind of mailbox you’re looking for is a property of the application, not an account.
Below will remove all messages from the first Recovered Messages mailbox the script finds. I don’t think you can script deleting mailboxes, and, if you want to erase the messages, you would need to empty the deleted items mailbox.
tell application "Mail"
set appMailBoxes to get mailboxes
repeat with x from 1 to count of appMailBoxes
set thisMailbox to item x of appMailBoxes
if name of thisMailbox contains "Recovered Messages" then
set recoveredMessages to messages of thisMailbox
if recoveredMessages is not {} then move recoveredMessages to mailbox "Deleted Messages"
exit repeat
end if
end repeat
end tell
I want to delete the whole mailbox itself, not empty it. Emptying it doesn’t solve the problem.
Here’s my current code:
tell application "Mail"
set appMailBoxes to get mailboxes
repeat with x from 1 to count of appMailBoxes
set thisMailbox to item x of appMailBoxes
if name of thisMailbox contains "Recovered Messages" then
delete thisMailbox
end if
end repeat
end tell
I’m getting an error on delete thisMailbox.
error "Mail got an error: AppleEvent handler failed." number -10000
I don’t know if that’s the case. It’s a folder that I can move emails into, and out of an account. The account is an IMAP situation, so moving an email into an application folder is just that.
But…this code here shows them as mailboxes:
tell application "Mail"
set appMailBoxes to get mailboxes
end tell
If there was a way to select the mailbox, I could pull a menu to delete it…which also has a dialog box.
Oh plenty of issues with this app. It’s been patched over and over for decades. This is the original Claris Mail, which became Outlook…it’s a messy bunch of stuff. But it’s what I’m using.
This doesn’t work and failed with “error “Mail got an error: AppleEvent handler failed.” number -10000”:
tell application “Mail”
if (exists mailbox “T_Accounting Emails/TEST1/Email Folders - AP/DISCOUNT1”) then
delete mailbox “T_Accounting Emails/TEST1/Email Folders - AP/DISCOUNT1”
end if
end tell
The mailbox exists as per commenting out the delete. Anyone ever get a delete of a nested mailbox to work? It seems from reviewing the web that this has been an ongoing problem for a long time.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on run
set mbox to "T_Accounting Emails/TEST1/Email Folders - AP/DISCOUNT1"
deleteMailbox(mbox)
end run
on deleteMailbox(mpath)
local mainWin, mboutline, myRows, aRow, aCell, found, disclosing, r
set tid to text item delimiters
set text item delimiters to "/"
set mpath to text items of mpath
tell application "System Events" to tell application process "Mail"
if not frontmost then set frontmost to true
try
set mainWin to window 1 whose description is "standard window"
on error
return
end try
set myRows to rows of outline 1 of scroll area 1 of splitter group 1 of mainWin
set rc to count myRows
set found to false
set r to 1
repeat while r ≤ rc
repeat with anMbox in mpath
set anMbox to contents of anMbox
set aRow to item r of myRows
set aCell to (UI element 1 of aRow whose role is "AXCell")
if exists UI element 2 of aCell then -- it has a disclosure triangle
--set disclosing to value of attribute "AXDisclosing" of aRow
if (value of attribute "AXDisclosing" of aRow) is false then
if not frontmost then
set frontmost to true
delay 0.2
end if
tell UI element 2 of aCell to perform action "AXPress" -- this will expand the mailbox to show sub-mailboxes
set myRows to rows of outline 1 of scroll area 1 of splitter group 1 of mainWin -- now there are more (visible) in the list, so recalculate
set rc to count myRows
end if
end if
set r to r + 1
if description of UI element 1 of aRow = anMbox then
if not found then set found to true
else
if found then set found to false
exit repeat
end if
get 1
end repeat
if found then
exit repeat
end if
end repeat
if found then
if selected of aRow is false then set selected of aRow to true
set mymenu to menu 1 of menu bar item "Mailbox" of menu bar 1
click menu item "Delete Mailbox…" of mymenu
repeat with i from 10 to 0 by -1
if exists (sheet 1 of mainWin whose description is "alert") then
set alertWin to (sheet 1 of mainWin whose description is "alert")
exit repeat
end if
delay 0.3
end repeat
if i > 0 then
click button "Delete" of alertWin
repeat with i from 10 to 0 by -1
if not (exists alertWin) then exit repeat
delay 0.3
end repeat
end if
end if
end tell
set text item delimiters to tid
end deleteMailbox
** EDIT ** - I cleaned up a little and fixed it where it only checks once if window exists