Skip to content

Instantly share code, notes, and snippets.

@logic2design
Created July 18, 2022 03:47
Show Gist options
  • Select an option

  • Save logic2design/996d2fb861a3bd081412df71598c8e8b to your computer and use it in GitHub Desktop.

Select an option

Save logic2design/996d2fb861a3bd081412df71598c8e8b to your computer and use it in GitHub Desktop.
This routine will cleanup mail in specified mailboxes after a specified number of days
##############################################
# Title: Cleanup Mailboxes
##############################################
# Iain Dunn
# Logic2Design
# www.logic2design.com
# logic2design@icloud.com
# Last update: 18 July 2022
# Version: 1
# Contributors and sources
#
##############################################
# Configuration
##############################################
set mailAccount1 to "iCloud" -- Email Account name
set mailAccount2 to "Gmail"
set mailAccount3 to "Outlook"
set folderName1 to "Receipts" -- Mailbox, use / to allow for sub folder structures
set folderName2 to "Categories/News"
set folderName3 to "Categories/Promos"
set delateAge1 to 7 -- number of days based on Received date to retain
set delateAge2 to 14
set delateAge3 to 30
set destination to "Trash" -- where Messages should be moved to
set theDate to current date
set speech to 1 -- Speak results, 1 to turn on, 0 to turn off
set userName to "Briaaan" -- name of User, phonetic spelling works best
##############################################
# Code
##############################################
--Option 1
tell application "Mail"
try
tell account mailAccount1 -- select mail account
set mailMove to mailbox destination
if speech = 1 then
set numMail to the count of (messages of mailbox folderName1 whose date received < ((theDate) - (days * delateAge1)))
say "There were" & numMail & "messages removed from the" & folderName1 & "mailbox" & userName
end if
set selectMessages to messages of mailbox folderName1 whose date received < ((theDate) - (days * delateAge1))
repeat with m in selectMessages
move m to mailMove
end repeat
end tell
end try
--Option 2
try
tell account mailAccount1 -- select mail account
set mailMove to mailbox destination
if speech = 1 then
set numMail to the count of (messages of mailbox folderName2 whose date received < ((theDate) - (days * delateAge2)))
say "There were" & numMail & "messages removed from the" & folderName2 & "mailbox " & userName
end if
set selectMessages to messages of mailbox folderName2 whose date received < ((theDate) - (days * delateAge2))
repeat with m in selectMessages
move m to mailMove
end repeat
end tell
end try
--Option 3
try
tell account mailAccount1 -- select mail account
set mailMove to mailbox destination
if speech = 1 then
set numMail to the count of (messages of mailbox folderName3 whose date received < ((theDate) - (days * delateAge3)))
say "There were" & numMail & "messages removed from the" & folderName3 & "mailbox" & userName
end if
set selectMessages to messages of mailbox folderName3 whose date received < ((theDate) - (days * delateAge3))
repeat with m in selectMessages
move m to mailMove
end repeat
end tell
end try
end tell
##############################################
# Functions
##############################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment