Skip to content

Instantly share code, notes, and snippets.

@logic2design
Created September 4, 2022 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logic2design/2a8042cb11f9b179203d10222034b96a to your computer and use it in GitHub Desktop.
Save logic2design/2a8042cb11f9b179203d10222034b96a to your computer and use it in GitHub Desktop.
This routine will move Messages from one folder to another if the Message is older than a nominated amount of days
##############################################
# Title: Purge Aged Mail
##############################################
# Iain Dunn
# Logic2Design
# www.logic2design.com
# logic2design@icloud.com
# Last update: 4 September 2022
# Version: 1
# Contributors and sources
# Jacques Rioux - https://discussions.apple.com/thread/7476557
##############################################
# Configuration
##############################################
set includeList to {"Promos", "Home", "News", "Test"}
set showMailboxesProgress to true -- display the "Processing" box displayed for each mailbox
set destFolderName to "Trash" -- mailbox to move messages to
set mAge to 14 -- mail older than this in days will be moved
##############################################
# Code
##############################################
tell application "Mail"
set everyAccount to every account
repeat with eachAccount in everyAccount
set everyMailbox to every mailbox of eachAccount
set accountName to the name of eachAccount
repeat with theMailbox in everyMailbox
set currentMailbox to theMailbox
set mailboxName to the name of currentMailbox as rich text
if mailboxName is in includeList then
if showMailboxesProgress then
display dialog "Processing aged messages in folder " & mailboxName & " in account " & accountName
end if
set mSource to mailbox mailboxName of account accountName
set mDestination to mailbox destFolderName of account accountName
my getSubMailboxes_moveMSGs(mSource, mDestination, mAge)
end if
end repeat
end repeat
end tell
##############################################
# Functions
##############################################
on getSubMailboxes_moveMSGs(s, d, a)
set processAge to (current date) - (a * days) -- days old the message must be before moved or deleted
tell application "Mail"
set messagesToDelete to messages of s whose date received ≤ processAge
repeat with aMessage in messagesToDelete
move aMessage to d
end repeat
repeat with thisSubMBox in (get mailboxes of s)
my getSubMailboxes_moveMSGs(thisSubMBox, d, a)
end repeat
end tell
end getSubMailboxes_moveMSGs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment