Apple Mail - Move If Spam

Summary: Of the selected messages, moves the spam ones to the Spam mailbox.
Requires: SpamSieve, Apple Mail, Panther
Install Location: ~/Library/Scripts/Mail Scripts/ on Panther or ~/Library/Scripts/Applications/Mail on Tiger

Description

Normally, to re-apply SpamSieve to a bunch of selected messages, you would use the Apply Rules command. This script is for when you want to re-apply SpamSieve without applying all the other rules.

Download in Compiled Format · Download in Text Format

Script

property markSpamMessagesRead : false

on run
    
tell application "Mail"
        
set _messages to the selection as list
        
my processMessages(_messages)
    
end tell
end run

on perform_mail_action(info)
    
tell application "Mail"
        
set _messages to |SelectedMessages| of info
        
my processMessages(_messages)
    
end tell
end perform_mail_action

on processMessages(_messages)
    
set _spamFolderName to getStringDefault("AppleMailTrainSpamName", "Spam")
    
repeat with _message in _messages
        
my processMessage(_message, _spamFolderName)
    
end repeat
end processMessages

on processMessage(_message, _spamFolderName)
    
tell application "Mail"
        
set _source to source of _message
        
tell application "SpamSieve"
            
set _isSpam to looks like spam message _source
        
end tell
        
if _isSpam then
            
my moveToSpamFolder(_message, _spamFolderName)
        
end if
    
end tell
end processMessage

on moveToSpamFolder(_message, _spamFolderName)
    
tell application "Mail"
        
try
            
set junk mail status of _message to true
        
end try
        
if markSpamMessagesRead then
            
try
                
set read status of _message to true
            
end try
        
end if
        
try
            
set _account to (account of mailbox of _message)
            
set mailbox of _message to (mailbox _spamFolderName of _account)
        
on error _error
            
try
                
if not (exists mailbox _spamFolderName) then
                    
make new mailbox with properties {name:_spamFolderName}
                
end if
                
set mailbox of _message to mailbox _spamFolderName
            
on error _error
                
display dialog _error
            
end try
        
end try
    
end tell
end moveToSpamFolder

on getStringDefault(_key, _defaultValue)
    
tell application "SpamSieve"
        
set _keys to {_key}
        
set _defaultValues to {_defaultValue}
        
try
            
set {_result} to lookup keys _keys default values _defaultValues
        
on error
            
set {_result} to _defaultValues
        
end try
        
return _result
    
end tell
end getStringDefault

Last Modified: 2012-03-26