Apple Mail - SaneBox

Summary: Automatically applies SpamSieve to unread messages in the @SaneLater and other mailboxes.
Requires: SpamSieve, Apple Mail, SaneBox
Install Location: ~/Library/Application Scripts/com.apple.mail or ~/Library/Scripts/Applications/Mail/
Last Modified: 2015-02-03

Description

The SaneBox service filters incoming messages into different mailboxes. This presents a problem because Apple Mail only knows how to apply its rules (such as SpamSieve) to new messages that arrive in the inbox. This script lets SpamSieve filter messages that arrive in other mailboxes such as @SaneLater.

First, download this script and enter your account and mailbox names where it says return {{"Account 1", {"@SaneLater"}}}.

Go to Mail’s Preferences window and create a new rule at the top of the list (above the SpamSieve rule) called SaneBox SpamSieve. The conditions should say:

Every Message

The actions should say:

Run AppleScript […]apple-mail-sanebox.scpt

After choosing Run AppleScript from the pop-up menu, select the apple-mail-sanebox.scpt file (using either the pop-up menu or the Choose… button).

Now, whenever you receive a new message in the inbox, SpamSieve will look in all the mailboxes that you specified and check all the unread messages. Any that are spam will be moved to the Spam mailbox.

Note: On OS X 10.8 Mountain Lion, the script must be saved in the folder /Users/<username>/Library/Application Scripts/com.apple.mail. This is because Mail is sandboxed and only has access to run scripts in that folder. To access the Library folder, click on the Go menu in the Finder while holding down the Option key.

Installation Instructions · Download in Compiled Format · Download in Text Format

Script

property pMarkSpamMessagesRead : false
property pColorSpamMessages : true
property pMoveBlueMessagesToTrash : false

on accountAndMailboxNames()
    
-- Enter your account name, followed by a list of mailboxes for that account that should be filtered.
    
-- The account name comes from the "Description" field in the Accounts tab of Mail's preferences.
    
-- For example, this would filter the @SaneLater mailbox of Account 1:
    
-- return {{"Account 1", {"@SaneLater"}}}
    
-- And this would filter the @SaneLater and Other mailboxes of the Personal account
    
-- and the @SaneLater mailbox of the Work account:
    
-- return {{"Personal", {"@SaneLater", "Other"}}, {"Work", {"@SaneLater"}}}
    
return {{"Account 1", {"@SaneLater"}}}
end accountAndMailboxNames

on run
    
-- This is executed when you run the script directly.
    
my filterSaneMailboxes()
end run

using terms from application "Mail"
    
on perform mail action with messages _messages
        
-- This is executed when Mail runs the rule.
        
my filterSaneMailboxes()
    
end perform mail action with messages
end using terms from

on filterSaneMailboxes()
    
tell application "Mail"
        
repeat with _pair in my accountAndMailboxNames()
            
set {_accountName, _mailboxNames} to _pair
            
try
                
set _account to account _accountName
                
repeat with _mailboxName in _mailboxNames
                    
my filterAccountMailboxName(_account, _mailboxName)
                
end repeat
            
on error _error
                
my logToConsole("Error checking mailbox “" & _mailboxName & "” from account “" & _accountName & "”: " & _error)
            
end try
        
end repeat
    
end tell
end filterSaneMailboxes

on filterAccountMailboxName(_account, _mailboxName)
    
tell application "Mail"
        
set _spamMailbox to my spamMailboxForAccount(_account)
        
set _mailbox to _account's mailbox _mailboxName
        
with timeout of 60 seconds
            
set _messages to messages of _mailbox whose read status is false and deleted status is false
        
end timeout
        
repeat with _message in _messages
            
my processMessageIfSpam(_message, _spamMailbox)
        
end repeat
    
end tell
end filterAccountMailboxName

on processMessageIfSpam(_message, _spamMailbox)
    
tell application "Mail"
        
set _source to _message's source
    
end tell
    
tell application "SpamSieve"
        
set _score to score message _source
    
end tell
    
tell application "Mail"
        
set _isSpam to _score ≥ 50
        
set _message's junk mail status to _isSpam
        
if _isSpam then
            
if pMarkSpamMessagesRead then
                
set _message's read status to true
            
end if
        
end if
        
if pColorSpamMessages then
            
if _score ≥ 99 then
                
set _message's background color to blue
            
else if _score ≥ 95 then
                
set _message's background color to gray
            
else if _score ≥ 88 then
                
set _message's background color to purple
            
else if _score ≥ 81 then
                
set _message's background color to red
            
else if _score ≥ 75 then
                
set _message's background color to orange
            
else if _score ≥ 50 then
                
set _message's background color to yellow
            
else
                
set _message's background color to none
            
end if
        
end if
        
if _isSpam then
            
if pMoveBlueMessagesToTrash and _score ≥ 99 then
                
delete _message
            
else
                
set _message's mailbox to _spamMailbox
            
end if
        
end if
        
return _isSpam
    
end tell
end processMessageIfSpam

on spamMailboxForAccount(_account)
    
tell application "Mail"
        
try
            
return mailbox "Spam" of _account
        
on error
            
return mailbox "Spam"
        
end try
    
end tell
end spamMailboxForAccount

on logToConsole(_message)
    
set _logMessage to "SpamSieve [Apple Mail SaneBox] " & _message
    
do shell script "/usr/bin/logger -s " & _logMessage's quoted form
end logToConsole