tell application "Mail"
set fetches automatically to false
set selectedMessages to (messages of mailbox "Spam Virus Mail" of mailbox "Personal" whose date received is less than (current date) - (2 * days))
set messcount to (count of selectedMessages)
if (count of selectedMessages) is equal to 0 then
tell application "Finder"
activate
display alert "No mail in the Spam Virus Mail mailbox is older than 2 days" message "Try running this script later on. Rob." giving up after 2
end tell
else
set deleteMessageFlag to my deleteMessages(0, 0)
if deleteMessageFlag > 0 then display dialog "Some messages could not be deleted" giving up after 5
end if
end tell
on delateMessages(loopCounter, errorMessage)
tell application "Mail"
set selectedMessages to (messages of mailbox "Spam Virus Mail" of mailbox "Personal" whose date received is less than (current date) - (2 * days))
repeat with i from messcount to 1 by -1
try
set theSource to item i of selectedMessages
set background color of theSource to red # What's the point of this???
delete theSource
on error Errmsg
set errorMessage to errorMessage + 1
end try
end repeat
set selectedMessages to (messages of mailbox "Spam Virus Mail" of mailbox "Personal" whose date received is less than (current date) - (2 * days))
end tell
set loopCounter to loopCounter + 1
if (count of selectedMessages) > 0 and loopCounter < 20 then my deleteMessages(loopCounter, errorMessage)
return errorMessage
end delateMessages