-- MailMaven - Filter Other Mailboxes -- https://c-command.com/scripts/spamsieve/mailmaven-filter-other-mailboxes -- Summary: Automatically applies SpamSieve to unread messages in mailboxes other than the inbox. -- Requires: SpamSieve, MailMaven -- Install Location: ~/Library/Application Scripts/com.smallcubed.mailmaven -- Last Modified: 2026-11-25 use AppleScript version "2.4" use framework "Foundation" use scripting additions global gDebug global gLogEntrySource global gKeyword on accountAndMailboxNames() -- Enter your account name, followed by a list of mailboxes for that account that should be filtered. -- For example, this would filter the @SaneLater mailbox of Account 1: -- return {{"Account 1", {"@SaneLater"}}} -- And this would filter the @SaneLater and Other mailboxes of the Personal account -- and the @SaneLater mailbox of the Work account: -- return {{"Personal", {"@SaneLater", "Other"}}, {"Work", {"@SaneLater"}}} return {{"Account 1", {"@SaneLater"}}} end accountAndMailboxNames on run -- This is executed when you run the script directly. my filterOtherMailboxes() end run on idle -- This is executed periodically when the script is run as a stay-open application. my filterOtherMailboxes() return 60 * 5 -- Run again in 5 minutes. end idle -- https://mailmaven.app/support/applescript/ using terms from application "MailMaven" to perform applescript action -- Ignore the messages passed to the rule. -- This is just a signal to check the pre-specified mailboxes. my filterOtherMailboxes() end perform applescript action end using terms from on filterOtherMailboxes() set gDebug to my lookupDefault("MailMavenScriptDebug", false) set gLogEntrySource to "com.c-command.spamsieve.mailmaven.script.filter-other-mailboxes" set gKeyword to "SpamSieveGood" if application "MailMaven" is not running then return tell application "MailMaven" repeat with _pair in my accountAndMailboxNames() set {_accountName, _mailboxNames} to _pair try set _account to account _accountName my debugLog("Start checking account: " & _accountName) repeat with _mailboxName in _mailboxNames try my filterAccountMailboxNamed(_account, _mailboxName) on error _error number _errorNumber my logToConsoleWithNumber(_errorNumber, "Error checking mailbox “" & _mailboxName & "” from account “" & _accountName & "”: " & _error) end try end repeat on error _error number _errorNumber my logToConsoleWithNumber(_errorNumber, "Error checking account “" & _accountName & "”: " & _error) end try end repeat end tell end filterOtherMailboxes on filterAccountMailboxNamed(_account, _mailboxName) tell application "MailMaven" my debugLog("Start checking mailbox: " & _mailboxName) set _mailbox to _account's mailbox _mailboxName set _total to count of messages of _mailbox -- where clause doesn't work: `and keywords does not contain "SpamSieveGood"` set _messages to messages of _mailbox whose read status is false and deleted status is false repeat with _message in _messages try if _message's keywords contains gKeyword then -- my debugLog("Skipping already processed message: " & my subjectFromMessage(_message)) else my processMessage(_message) end if on error _error number _errorNumber my logToConsoleWithNumber(_errorNumber, "Error processing message “" & my subjectFromMessage(_message) & "”: " & _error) end try end repeat my debugLog("Finished checking mailbox: " & _mailboxName) end tell end filterAccountMailboxNamed on processMessage(_message) tell application "SpamSieve" set _score to score message my sourceFromMessage(_message) origin my originForMessage(_message) with Outlook source end tell tell application "MailMaven" my debugLog("Spam score of message is " & _score & ": " & my subjectFromMessage(_message)) set _isSpam to _score ≥ 50 if _isSpam then set _account to account of _message's mailbox move _message to junk mailbox of _account else set _message's keywords to _message's keywords & {gKeyword} end if end tell end processMessage on sourceFromMessage(_message) tell application "MailMaven" -- `source` may be lossy; convert raw data to MacRoman mojibake like Outlook gives us set _rawData to _message's source data set _nsData to (current application's NSArray's arrayWithObject:_rawData)'s firstObject()'s |data|() set _nsString to current application's NSString's alloc()'s initWithData:_nsData encoding:(current application's NSMacOSRomanStringEncoding) return _nsString as text end tell end sourceFromMessage on subjectFromMessage(_message) tell application "MailMaven" set _subject to _message's subject return _subject as Unicode text -- Otherwise errAECoercionFail end tell end subjectFromMessage on originForMessage(_message) tell application "MailMaven" set _appName to "MailMaven" set _appVersion to version set _mailbox to _message's mailbox set _account to _mailbox's account set _accountName to name of _account set _mailboxName to name of _mailbox try set _mailboxContainer to _mailbox's parent -- Currently this inappropriately returns `missing value` set _mailboxContainerName to name of _mailboxContainer on error set _mailboxContainerName to "" end try end tell tell application "SpamSieve" set _origin to {source identifier:gLogEntrySource} set _origin to _origin & {application name:_appName} set _origin to _origin & {application version:_appVersion} -- MailMaven does not make account type available -- set _origin to _origin & {mail account type:"IMAP"} set _origin to _origin & {mail account name:_accountName} set _origin to _origin & {mailbox name:_mailboxName} set _origin to _origin & {mailbox container name:_mailboxContainerName} return _origin end tell end originForMessage on lookupDefault(_key, _defaultValue) try tell application "SpamSieve" lookup single key _key default value _defaultValue end tell on error _errorMessage number _errorNumber my logToConsoleWithNumber(_errorNumber, "Error reading default: " & _errorMessage & " key:" & _key) return _defaultValue end try end lookupDefault on debugLog(_message) if gDebug then try tell application "SpamSieve" add log entry area "MailMavenScriptDebug" source gLogEntrySource message _message end tell end try end if end debugLog on logToConsole(_message) try tell application "SpamSieve" add log entry source gLogEntrySource message _message end tell end try end logToConsole on logToConsoleWithNumber(_errorNumber, _message) try tell application "SpamSieve" add log entry source gLogEntrySource error number _errorNumber message _message end tell end try end logToConsoleWithNumber