This question has plagued me for 10 years.
The only way of reliably resetting a mailbox is to quit Mail, and re-start it.
1. Create two new mailboxes ‘items to shift’ & ‘items to store’.
2. Create a mail rule that shifts All incoming mail from your inbox to ‘items to shift’.
3. when your script/App starts, move the mail from ‘items to shift’ to ‘items to store’
4. Count the items in ‘items to store’.
5. Deal with the FIRST item (oldest) in 'items to store', and move it.
6. In a timed loop, count the items in ‘items to store’.
7. If the count decreases by one before the looping ends, deal with the next item.
8. If the loop times out without a decrease, quit Mail, and restart it.
9. While quitting Mail, check that it’s actually quit before restarting, or issue a force quit.
10. When the count of ‘items to store’ reaches 0, check for and move any mail from ‘items to shift’ again.
Here’s some handlers you might find useful.
on mailRuleCreator:sender
# say 503
set isThisStartUp to false #forces reset of rule every time this is run
tell application "Mail"
activate
try
tell current application to delay 0.2
tell application "System Events" to tell process "Mail"
click menu item "Message Viewer" of menu 1 of menu bar item "Window" of menu bar 1
end tell
if not (exists mailbox "* items to shift") then make new mailbox with properties {name:"* items to shift"}
set theAddRuleMailbox to false
if not (exists rule "Move to '* items to shift'") or not isThisStartUp then
set inBoxList to name of every account whose enabled is true as list
if (count of inBoxList) = 1 then
set theAddRuleMailbox to item 1 of inBoxList
else
set theAddRuleMailbox to choose from list inBoxList with prompt "Please choose an Account to which to add the Mail rule " & return & return & "\"Move to '* items to shift'\"." & return with title "Mail Manager add Mail rule" OK button name "Choose this Inbox Account" cancel button name "I'll set the rule myself"
end if
end if
if theAddRuleMailbox ≠ false then
try
delete (every rule where name contains "items to shift")
end try
try
delete (every rule where name contains "Autom")
end try
if not (exists rule "Move to '* items to shift'") then
tell application "System Events" to tell process "Mail"
try
keystroke return
end try
click menu item "Preferences…" of menu 1 of menu bar item "Mail" of menu bar 1
tell current application to delay 0.1
click button 8 of toolbar 1 of window 1
#
# Set descriptive name of rule
#
click button "Add Rule" of group 1 of group 1 of window "Rules"
set value of text field 1 of sheet 1 of window "Rules" to "Move to '* items to shift'"
#
# First, select the 'all' menu item
#
click pop up button 1 of sheet 1 of window "Rules"
click menu item "all" of menu 1 of pop up button 1 of sheet 1 of window "Rules"
#
# Set first scroll area, 'Conditions'
#
click pop up button 1 of scroll area 1 of sheet 1 of window "Rules"
# The darn popups change numbers whilst 'Account' is being selected
try
click menu item "Account" of menu 1 of pop up button 1 of scroll area 1 of sheet 1 of window "Rules"
tell current application to delay 0.1
end try
# so we have to try both 1 & 2 popups
try
click menu item "Account" of menu 1 of pop up button 2 of scroll area 1 of sheet 1 of window "Rules"
tell current application to delay 0.1
end try
click pop up button 1 of scroll area 1 of sheet 1 of window "Rules"
tell current application to delay 0.1
click menu item (theAddRuleMailbox as text) of menu 1 of pop up button 1 of scroll area 1 of sheet 1 of window "Rules"
tell current application to delay 0.1
#
click button "add criterion" of scroll area 1 of sheet 1 of window "Rules"
#
click pop up button 3 of scroll area 1 of sheet 1 of window "Rules"
click menu item "Every Message" of menu 1 of pop up button 3 of scroll area 1 of sheet 1 of window "Rules"
tell current application to delay 0.1
#
# Set second scroll area, 'Actions'
#
click pop up button 1 of scroll area 2 of sheet 1 of window "Rules"
tell current application to delay 0.1
click menu item "Move Message" of menu 1 of pop up button 1 of scroll area 2 of sheet 1 of window "Rules"
tell current application to delay 0.1
click pop up button 2 of scroll area 2 of sheet 1 of window "Rules"
tell current application to delay 0.1
click menu item "* items to shift" of menu 1 of pop up button 2 of scroll area 2 of sheet 1 of window "Rules"
tell current application to delay 0.1
#
click button "add action" of scroll area 2 of sheet 1 of window "Rules"
tell current application to delay 0.1
#
click pop up button 3 of scroll area 2 of sheet 1 of window "Rules"
tell current application to delay 0.1
click menu item "Run Applescript" of menu 1 of pop up button 3 of scroll area 2 of sheet 1 of window "Rules"
tell current application to delay 0.1
click pop up button 4 of scroll area 2 of sheet 1 of window "Rules"
tell current application to delay 0.1
try
click menu item "Mail Manager Caller" of menu 1 of pop up button 4 of scroll area 2 of sheet 1 of window "Rules"
on error
try
click menu item 1 of menu 1 of pop up button 4 of scroll area 2 of sheet 1 of window "Rules"
end try
end try
tell current application to delay 0.1
click button "OK" of sheet 1 of window "Rules"
tell current application to delay 0.1
try
keystroke return
end try
tell current application to delay 0.1
try
click button 1 of window 1
end try
end tell
end if
end if
on error errmsg number errNum
tell application "System Events" to tell process "Mail"
try
tell current application to delay 0.1
click button "OK" of sheet 1 of window "Rules"
end try
try
click button 1 of window 1
end try
end tell
display dialog "mailRuleCreator error " & errmsg
end try
end tell
end mailRuleCreator:
To force Quit
on MM_Quitter()
if my voiceSetter as text is "Use voice 'Vicki'" then
say ("Mail Manager is turning off") using my TheVoice
else
say ("Mail Manager is turning off")
end if
set The_app to "Mail Manager"
try
tell application "Finder"
tell application "System Events" to set pid to the unix id of process The_app as Unicode text
do shell script "kill " & pid
end tell
end try
end MM_Quitter