-- Apple Mail - Mark Server Spam Messages as Read -- https://c-command.com/scripts/spamsieve/apple-mail-mark-server-spam-messages-read -- Summary: Marks all the messages in the server Spam/Junk mailboxes as read -- Requires: SpamSieve, Apple Mail -- Install Location: ~/Library/Application Scripts/com.apple.mail or ~/Library/Scripts/Applications/Mail/ -- Last Modified: 2019-10-02 property pEnableDebugLogging : false on accountAndServerJunkMailboxNames() return {{"Account 1", {"Spam"}}} end accountAndServerJunkMailboxNames -- Do not modify below this line. on run -- This is executed when you run the script directly. my markServerSpamMessagesAsRead() end run on idle -- This is executed periodically when the script is run as a stay-open application. my markServerSpamMessagesAsRead() 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 markServerSpamMessagesAsRead() end perform mail action with messages end using terms from on markServerSpamMessagesAsRead() tell application "System Events" if not (exists process "Mail") then return end tell 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 processAccountMailboxNamed(_account, _mailboxName) on error _error number _errorNumber my logToConsole("Error " & _errorNumber & " filtering processing “" & _mailboxName & "” of account “" & _accountName & "”: " & _error) end try end repeat end repeat on error _error my logToConsole("Error processing junk mailboxes: " & _error) end try end tell end markServerSpamMessagesAsRead on processAccountMailboxNamed(_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)) repeat with _message in _messages set _message's read status to true end repeat my debugLog(my makeLogMessage("Finished checking mailbox", _mailbox, "")) end tell end processAccountMailboxNamed -- Logging on debugLog(_message) if pEnableDebugLogging then my logToConsole(_message) end debugLog on logToConsole(_message) set _logMessage to "SpamSieve [Apple Mail Mark Server Spam Messages as Read] " & _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