Apple Mail - Mark Server Spam Messages as Read

Summary: Marks all the messages in the server Spam/Junk mailboxes as read
Requires: SpamSieve, Apple Mail
Install Location: ~/Library/Application Scripts/ or ~/Library/Scripts/Applications/Mail/
Last Modified: 2016-09-20


You can set SpamSieve to mark the spam messages that it catches as read. However, you may have a server junk filter that catches some of the spams before SpamSieve sees them. In that case, you can use this script if you want the spam messages to be automatically marked as read so that Mail does not show a numbered badge next to the mailbox.

To install:

  1. Download the script using one of the links below. On Mac OS X 10.8 or later, the script must be saved in the folder /Users/<username>/Library/Application Scripts/ This is because Mail is sandboxed and only has access to run scripts in that folder. To access the Library folder, click on the Go menu in the Finder while holding down the Option key.
  2. Open the script in AppleScript Editor and enter your account and junk mailbox names in accountAndServerJunkMailboxNames(). The account name comes from the Description field in the Accounts tab of Mail’s preferences. For example, if you have one account called Account 1 and want to process its Junk mailbox, you would use:

    on accountAndServerJunkMailboxNames()
        return {{"Account 1", {"Junk"}}}
    end accountAndServerJunkMailboxNames

    You can also get the AppleScript names using the Apple Mail - List Mailboxes script.

    If you want to process the Junk mailbox of the Personal account and the Junk and Bulk mailboxes of the Work account you would use:

    on accountAndServerJunkMailboxNames()
        return {{"Personal", {"Junk"}}, {"Work", {"Junk", "Bulk"}}}
    end accountAndServerJunkMailboxNames
  3. Go to Mail’s Preferences window and create a new rule at the top of the list (above the SpamSieve rule) called Mark Server Spam Messages as Read.
  4. The rule conditions should say Every Message.
  5. The rule actions should say Run AppleScript […]Apple Mail - Mark Server Spam Messages as Read.scpt. First, choose Run AppleScript from the pop-up menu; then select the Apple Mail - Mark Server Spam Messages as Read.scpt file (using either the pop-up menu or the Choose… button).

Now, whenever you receive a new message in the inbox, SpamSieve will look in all the server junk mailboxes that you specified and mark the messages as read

Normally, the script will run when Mail receives a new message in the inbox and applies the “Mark Server Spam Messages as Read” rule. You can also set up the script as a standalone application. This has less overhead than running the script from within Mail, and it allows the script to run on a more predictable schedule. To do this, use Script Editor to export the script as an application (with the Stay Open option checked) and then launch the application. You can set in the “idle” handler how often it should run.

To test this script you can run it in Script Editor (a.k.a. AppleScript Editor) and look for any error messages in the Console application. You can also enable debug logging by changing pEnableDebugLogging from false to true.

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


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"
repeat with _pair in my accountAndServerJunkMailboxNames()
set {_accountName, _mailboxNames} to _pair
set _account to account _accountName
repeat with _mailboxName in _mailboxNames
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
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