Outlook - Remote Training

Summary: Training script for setting up a spam filtering drone with Outlook.
Requires: SpamSieve, Outlook
Install Location: ~/Library/Application Support/Microsoft/Office/Outlook Script Menu Items/
Last Modified: 2016-04-27

Description

Please see the Setting Up a Spam Filtering Drone section of the manual. You’ll need to enter the names of the IMAP and Exchange accounts that you want the drone to operate on.

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

Script

-- Enter your account names here. If you have more than one, separate with commas: {"Account 1", "Account 2"}
-- The account name comes from the "Account description" field in the Accounts pane of Outlook's preferences.
set _imapAccountNames to {"Account 1"}
set _exchangeAccountNames to {}

tell application "Microsoft Outlook"
    
repeat with _accountName in _imapAccountNames
        
set _account to imap account _accountName
        
my trainMessagesInAccount(_account)
    
end repeat
    
repeat with _accountName in _exchangeAccountNames
        
set _account to exchange account _accountName
        
my trainMessagesInAccount(_account)
    
end repeat
end tell

on trainMessagesInAccount(_account)
    
tell application "Microsoft Outlook"
        
set _folder to folder "TrainGood" of _account
        
set _messages to incoming messages of _folder
        
repeat with _message in _messages
            
set _source to _message's source
            
tell application "SpamSieve" to add good message _source
            
my removeJunkCategories(_message)
            
move _message to inbox of _account
        
end repeat
        
        
set _folder to folder "TrainSpam" of _account
        
set _messages to incoming messages of _folder
        
repeat with _message in _messages
            
set _source to _message's source
            
tell application "SpamSieve" to add spam message _source
            
my applyCategoryNamed(_message, "Junk")
            
my moveToSpamFolder(_message)
        
end repeat
    
end tell
end trainMessagesInAccount

on categoryForName(_categoryName)
    
tell application "Microsoft Outlook"
        
try
            
return category _categoryName
        
on error
            
try
                
-- Getting by name doesn't always work.
                
repeat with _category in categories
                    
if _category's name is _categoryName then return _category
                
end repeat
            
end try
            
make new category with properties {name:_categoryName}
        
end try
        
return category _categoryName
    
end tell
end categoryForName

on applyCategoryNamed(_message, _categoryName)
    
tell application "Microsoft Outlook"
        
set _categoryToApply to my categoryForName(_categoryName)
        
set _categories to _message's category
        
repeat with _category in _categories
            
if _category's id is equal to _categoryToApply's id then return
        
end repeat
        
set category of _message to {_categoryToApply} & category of _message
    
end tell
end applyCategoryNamed

on removeJunkCategories(_message)
    
tell application "Microsoft Outlook"
        
try
            
set _categories to _message's category
            
set _newCategories to {}
            
repeat with _category in _categories
                
if _category's name is not in {"Junk", "Uncertain Junk"} then
                    
copy _category to end of _newCategories
                
end if
            
end repeat
            
set _message's category to _newCategories
        
end try
    
end tell
end removeJunkCategories

on moveToSpamFolder(_message)
    
tell application "Microsoft Outlook"
        
set _destFolder to my junkFolderForMessage(_message)
        
move _message to _destFolder
        
try
            
if _message's folder is not _destFolder then
                
move _message to junk mail
            
end if
        
end try
    
end tell
end moveToSpamFolder

on junkFolderForMessage(_message)
    
tell application "Microsoft Outlook"
        
try
            
set _destFolder to junk mail of _message's account
            
if _destFolder is not missing value then return _destFolder
        
end try
        
try
            
set _destFolder to junk mail
            
if _destFolder is not missing value then return _destFolder
        
end try
        
try
            
set _destFolder to folder "Junk E-mail" of _message's account
            
if _destFolder is not missing value then return _destFolder
        
end try
        
try
            
set _destFolder to folder "Junk" of _message's account
            
if _destFolder is not missing value then return _destFolder
        
end try
        
display dialog "Could not find the “Junk E-mail” folder"
        
return junk mail
    
end tell
end junkFolderForMessage