Apple Mail - Save Spam

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

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.

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

Script

on getSpamFolder()
    
set _spamFolderName to my lookupDefaults({"AppleMailTrainSpamName"}, {"Spam"})
    
tell application "Mail"
        
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

on lookupDefaults(_keys, _defaultValues)
    
tell application "SpamSieve"
        
try
            
set _result to {}
            
repeat with _i from 1 to count of _keys
                
set _key to item _i of _keys
                
set _defaultValue to item _i of _defaultValues
                
set _value to lookup single key _key default value _defaultValue
                
copy _value to end of _result
            
end repeat
            
return _result
        
on error -- SpamSieve 2.9.15 and earlier
            
return lookup keys _keys default values _defaultValues
        
end try
    
end tell
end lookupDefaults