Import/Export Rules

Summary: Demonstrates how to import and export SpamSieve’s rules via AppleScript.
Requires: SpamSieve
Install Location: wherever
Last Modified: 2017-03-03

Description

You can copy all your blocklist and whitelist rules to another Mac by following the instructions here.

Running this script will export certain blocklist rules (from address rules with “ends with,” “starts with,” and “regex” styles) to the clipboard. Paste the clipboard into a new script in AppleScript Editor. When you run that script, it will re-create the rules in SpamSieve (without overwriting the existing rules).

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

Script

on run
    
tell application "SpamSieve"
        
set importScript to "tell application \"SpamSieve\"" & return
        
set importScript to importScript & "tell blocklist" & return
        
set importScript to importScript & my makeRules(blocklist, ends with style, "ends with style", from field, "from field")
        
set importScript to importScript & my makeRules(blocklist, starts with style, "starts with style", from field, "from field")
        
set importScript to importScript & my makeRules(blocklist, regex style, "regex style", from field, "from field")
        
set importScript to importScript & "end tell" & return
        
set importScript to importScript & "end tell"
        
set the clipboard to importScript
    
end tell
end run

on makeRules(theList, theStyle, theStyleText, theField, theFieldText)
    
tell application "SpamSieve"
        
tell theList
            
set theResult to ""
            
set theRules to get every rule whose match style is theStyle and match field is theField
            
repeat with r in theRules
                
set theResult to theResult & my makeRule(r's text to match, theStyleText, theFieldText)
            
end repeat
        
end tell
    
end tell
    
return theResult
end makeRules

on makeRule(theText, theStyle, theField)
    
return "make rule with properties {text to match: " & my escape(theText) & ", match field: " & theField & ", match style: " & theStyle & "}" & return
end makeRule

on escape(theText)
    
set bs to "\\"
    
set q to "\""
    
set theText to my replace(theText, bs, bs & bs)
    
set theText to my replace(theText, q, bs & q)
    
return q & theText & q
end escape

on replace(theText, searchText, replaceText)
    
set AppleScript's text item delimiters to searchText
    
set i to text items of theText
    
set AppleScript's text item delimiters to replaceText
    
return i as string
end replace