Whitelist Rules to Address Book

Summary: Creates address book cards based on whitelist rules.
Requires: SpamSieve, Mac OS X Address Book
Install Location: Scripts menu
Last Modified: 2020-09-21

Description

Run this script with some rules in SpamSieve’s whitelist selected. If any of the selected rules are exact matches for e-mail addresses (e.g. “From (address) Is Equal To” but not “Subject Starts With”), and these addresses are not already present in the address book, it will create new address book cards for them.

This script demonstrates the use of the selection scripting property. If the whitelist or blocklist window is frontmost, selection refers to the selected rules. If the corpus window is frontmost, it refers to the selected token info objects.

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

Script

global gAddresses
set gAddresses to {}

on loadAddressesIfNecessary()
    
if gAddresses is not {} then return
    
tell application "Contacts"
        
repeat with thePerson in people
            
repeat with theEmail in every email of thePerson
                
set theAddress to value of theEmail
                
copy theAddress to end of gAddresses
            
end repeat
        
end repeat
        
return false
    
end tell
end loadAddressesIfNecessary

on addressBookContainsEmail(theAddress)
    
my loadAddressesIfNecessary()
    
ignoring case
        
return gAddresses contains theAddress
    
end ignoring
end addressBookContainsEmail

on makeNewAddressBookCard(emailAddress)
    
tell application "Contacts"
        
set newEntry to make new person
        
tell newEntry
            
make new email at beginning of emails with properties {value:emailAddress}
        
end tell
    
end tell
end makeNewAddressBookCard

on ruleHasFullAddress(theRule)
    
tell application "SpamSieve"
        
if theRule's match style is not exact style then return false
        
set theFields to {from field, reply to field, to field, any address, cc field, any recipient}
        
return theRule's match field is in theFields
    
end tell
end ruleHasFullAddress

tell application "SpamSieve"
    
set theRules to selection
    
repeat with theRule in theRules
        
if my ruleHasFullAddress(theRule) then
            
set theAddress to text to match of theRule
            
if not my addressBookContainsEmail(theAddress) then
                
my makeNewAddressBookCard(theAddress)
            
end if
        
end if
    
end repeat
end tell

tell application "Contacts"
    
save
end tell