Deleting old spam messages with AppleScript

Recently, I blogged about setting up my own “server-side” spam filtering system based on a home-based iMac running Mail with SpamSieve. I’m super-happy with the setup, as it’s keeping my inbox spam-free on my MacBook Air, iPhone and iPad.

The only feature I found missing in this setup, with respect to Apple’s own junk mail filter, was the ability to automatically delete old spam messages after a certain number of days.

The SpamSieve manual describes one option for doing this, but it involves renaming your Spam folder, re-enabling Mail’s junk mail feature, and setting some advanced options.

What I really wanted was an AppleScript that I could schedule to run periodically from Keyboard Maestro, that would automatically delete messages in my Spam folder older than 30 days.

Doing some Googling, I found this article at Macworld, that referenced an AppleScript for generally moving messages between folders. I hacked and simplified the script as follows:

  • It cleans only one folder, my Spam folder, rather than a list of folders.
  • It sends a message to Growl, alerting me to the number of messages that were deleted.

I setup a Keyboard Maestro task to run this script each morning at 6:00 am, and it’s working great!

In case you’re interested, here’s the AppleScript. (And for the record, I know very little about AppleScript, so I’ll update this code as soon as the wizards out there point out the problems/improvements that surely exist.)

set DestinFolderName to "" -- mailbox to move messages to. If you want to just delete them, leave it blank.
set StaleTime to 30 -- days old the message must be before moved or deleted
set ShowMailboxesProgress to false -- determines if you want the "Processing" box displayed for each mailbox
set CountMessages to 0

tell application "Mail"
    set thisAccount to account "Your IMAP or POP Account Name"
    set accountName to the name of thisAccount
    set mailboxName to the name of mailbox "Spam" in thisAccount
    if ShowMailboxesProgress then
        display dialog "Processing folder " & mailboxName & " in account " & accountName
    end if
        set messages_list to every message of mailbox mailboxName in thisAccount
        set NumItems to number of items in messages_list
        -- display dialog "Number of items" & NumItems
        repeat with i from 1 to number of items in messages_list
            set theMessage to item i of messages_list
            set difference to ((current date) - (date sent of theMessage)) div days
            if difference is greater than StaleTime then
                if DestinFolderName is not equal to "" then
                    move theMessage to mailbox DestinFolderName in thisAccount
                    delete theMessage
                    set CountMessages to CountMessages + 1
                end if

            end if
        end repeat
    end try
    tell application "Growl"
        set the allNotificationsList to {"Cleaned up spam folder"}
        set the enabledNotificationsList to {"Cleaned up spam folder"}
        register as application "Spam Folder Cleanup" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "SpamSieve"
        notify with name "Cleaned up spam folder" title "Cleaned up spam folder" description "I just deleted " & CountMessages & " messages in the Spam folder older than " & StaleTime & " days." application name "Spam Folder Cleanup"
    end tell
    -- display dialog "Finished!"
end tell

Agree? Disagree? What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.