Apple Mail - Save Spam

Summary: Saves messages to a Spam folder on the desktop.
Requires: Apple Mail, SpamSieve
Install Location: ~/Library/Scripts/Applications/Mail/

Description

This script saves the messages in the spam mailbox to a Spam folder on the desktop. Each message’s raw source is saved as a separate file.

Download in Compiled Format | Download in Text Format

Script

on getSpamFolder()
    
tell application "SpamSieve"
        
set theKeys to {"AppleMailTrainSpamName"}
        
set theDefaults to {"Spam"}
        
set {spamFolderName} to lookup keys theKeys default values theDefaults
    
end tell
    
tell application "Mail"
        
if spamFolderName is "" then return
        
return mailbox spamFolderName
    
end tell
end getSpamFolder

on writeToFileNamed(s, theName)
    
set theFolder to do shell script "echo ~/Desktop/Spam/"
    
try
        
do shell script "mkdir " & theFolder's quoted form
    
end try
    
set theFile to theFolder & theName
    
try
        
do shell script "echo " & s's quoted form & ">" & theFile's quoted form
    
end try
end writeToFileNamed

tell application "Mail"
    
set theMessages to every message of my getSpamFolder()
    
repeat with m in theMessages
        
my writeToFileNamed(m's source, "Message " & m's id)
        --
delete m
    
end repeat
end tell