-- Apple Mail - Server Junk Mailbox -- https://c-command.com/scripts/spamsieve/apple-mail-server-junk-mailbox -- Summary: Automatically applies SpamSieve to unread messages in server Junk mailboxes. -- Requires: SpamSieve, Apple Mail -- Install Location: ~/Library/Application Scripts/com.apple.mail or ~/Library/Scripts/Applications/Mail/ -- Last Modified: 2021-09-20 property pMarkSpamMessagesRead : false property pMarkGoodMessagesRead : false property pChangeJunkStatus : true property pColorSpamMessages : true property pFlagSpamMessages : false property pMoveBlueMessagesToTrash : false property pMoveGrayMessagesToTrash : false property pMovePurpleMessagesToTrash : false property pMoveRedMessagesToTrash : false property pMoveOrangeMessagesToTrash : false property pMoveYellowMessagesToTrash : false property pPerAccountSpamMailboxes : false property pOnlyMarkSpamAsRead : false property pEnableDebugLogging : false on accountAndServerJunkMailboxNames() return {{"Account 1", {"Junk"}}} end accountAndServerJunkMailboxNames on mainSpamMailboxInfo() return {"", "Spam"} end mainSpamMailboxInfo -- Do not modify below this line. on run -- This is executed when you run the script directly. my filterServerJunkMailboxes() end run on idle -- This is executed periodically when the script is run as a stay-open application. try my filterServerJunkMailboxes() on error _error my logToConsole("Error re-checking server junk mailboxes: " & _error) end try return 60 * 5 -- Run again in 5 minutes. end idle using terms from application "Mail" on perform mail action with messages _messages -- This is executed when Mail runs the rule. my filterServerJunkMailboxes() end perform mail action with messages end using terms from on filterServerJunkMailboxes() if application "Mail" is not running then return try tell application "Mail" to get version on error _error number _errorNumber if _errorNumber is -1743 then -- errAEEventNotPermitted set _alertMessage to "You can give “Apple Mail - Server Junk Mailbox” access to control Mail and SpamSieve from System Preferences > Security & Privacy > Privacy > Automation. For more information, please see: https://c-command.com/spamsieve/help/security-privacy-acce" display alert _error message _alertMessage end if end try tell application "Mail" try repeat with _pair in my accountAndServerJunkMailboxNames() set {_accountName, _mailboxNames} to _pair set _account to account _accountName repeat with _mailboxName in _mailboxNames try my filterAccountMailboxNamed(_account, _mailboxName) on error _error number _errorNumber my logToConsole("Error " & _errorNumber & " filtering mailbox “" & _mailboxName & "” of account “" & _accountName & "”: " & _error) end try end repeat end repeat on error _error my logToConsole("Error filtering junk mailboxes: " & _error) end try end tell end filterServerJunkMailboxes on filterAccountMailboxNamed(_account, _mailboxName) tell application "Mail" set _mailbox to _account's mailbox _mailboxName my debugLog(my makeLogMessage("Start checking mailbox", _mailbox, "")) set _total to count of messages of _mailbox my debugLog(my makeLogMessage("Total messages in mailbox", _mailbox, _total)) my debugLog(my makeLogMessage("Getting unread, non-deleted messages in mailbox", _mailbox, "")) with timeout of 3 * 60 seconds set _messages to messages of _mailbox whose read status is false and deleted status is false end timeout my debugLog(my makeLogMessage("Messages to process in mailbox", _mailbox, count of _messages)) set _spamMailbox to my spamMailboxForAccount(_account) set _inbox to my inboxFromAccount(_account) repeat with _message in _messages if not my processMessageIfSpam(_message, _spamMailbox) then if pMarkGoodMessagesRead then set _message's read status to true my moveMessage(_message, _inbox) end if end repeat my debugLog(my makeLogMessage("Finished checking mailbox", _mailbox, "")) end tell end filterAccountMailboxNamed on processMessageIfSpam(_message, _spamMailbox) set _source to my sourceFromMessage(_message) tell application "SpamSieve" set _score to score message _source end tell tell application "Mail" my debugLog("Spam score of message is " & _score & ": " & _message's subject) set _isSpam to _score ≥ 50 if pChangeJunkStatus then set _message's junk mail status to _isSpam end if if _isSpam and (pMarkSpamMessagesRead or pOnlyMarkSpamAsRead) then set _message's read status to true end if if not pOnlyMarkSpamAsRead then set _moveToTrash to my colorMessageAndDecideIfShouldMoveToTrash(_message, _score) if _moveToTrash then delete _message else if _isSpam then my moveMessage(_message, _spamMailbox) end if end if return _isSpam end tell end processMessageIfSpam on colorMessageAndDecideIfShouldMoveToTrash(_message, _score) tell application "Mail" set _table to {¬ {99, blue, pMoveBlueMessagesToTrash, 6}, ¬ {95, gray, pMoveGrayMessagesToTrash, 5}, ¬ {88, purple, pMovePurpleMessagesToTrash, 4}, ¬ {81, red, pMoveRedMessagesToTrash, 3}, ¬ {75, orange, pMoveOrangeMessagesToTrash, 2}, ¬ {50, yellow, pMoveYellowMessagesToTrash, 1}, ¬ {0, none, false, -1}} -- Flag colors chosen so that messages sort by spamminess: gray, purple, blue, green, yellow, orange, none repeat with _row in _table set {_threshold, _color, _moveToTrash, _flagColor} to _row if _score ≥ _threshold then if pColorSpamMessages then set _message's background color to _color end if if pFlagSpamMessages then set _message's flag index to _flagColor end if return _moveToTrash end if end repeat end tell end colorMessageAndDecideIfShouldMoveToTrash on spamMailboxForAccount(_account) if pPerAccountSpamMailboxes then tell application "Mail" set {_accountName, _mailboxName} to my mainSpamMailboxInfo() try return mailbox _mailboxName of _account on error _error my logToConsole("Error getting per-account spam mailbox: " & _mailboxName) end try end tell end if return my mainSpamMailbox() end spamMailboxForAccount on mainSpamMailbox() set {_accountName, _mailboxName} to my mainSpamMailboxInfo() tell application "Mail" if _accountName is "" then return mailbox _mailboxName else return mailbox _mailboxName of account _accountName end if end tell end mainSpamMailbox on inboxFromAccount(_account) tell application "Mail" set _names to {"INBOX", "Inbox", "innboks", "Posteingang", "Boite de reception"} repeat with _name in _names try set _mailbox to mailbox _name of _account return _mailbox end try end repeat return inbox end tell end inboxFromAccount -- Logging on debugLog(_message) if pEnableDebugLogging then my logToConsole(_message) end debugLog on logToConsole(_message) set _logMessage to "SpamSieve [Apple Mail Server Junk Mailbox MJTLog] " & _message do shell script "/usr/bin/logger -s " & _logMessage's quoted form end logToConsole on makeLogMessage(_action, _mailbox, _detail) return _action & " " & my describeMailbox(_mailbox) & ": " & _detail end makeLogMessage on describeMailbox(_mailbox) tell application "Mail" set _mailboxName to _mailbox's name try set _accountName to name of _mailbox's account on error set _accountName to "On My Mac" end try return "“" & _accountName & "” / “" & _mailboxName & "”" end tell end describeMailbox -- Logging Helpers on sourceFromMessage(_message) tell application "Mail" my debugLog(my makeLogMessage("Getting source of message in", _message's mailbox, _message's subject)) return _message's source end tell end sourceFromMessage on moveMessage(_message, _mailbox) tell application "Mail" my debugLog(my makeLogMessage("Moving message to", _mailbox, _message's subject)) set _message's mailbox to _mailbox end tell end moveMessage