Is there any way to process emails in Junk mailbox?

The modified script is great at moving all of the identified spam to the trash and marking it read. Thanks - huge relief.

In case anyone is reading this later, I commented out the below lines in order to leave the “Good” messages in the junk folder. Now that it is nice and empty, it is easier for me to review them there than to find them in my inbox (which is organized by date received so they are interleaved in).

	--				my debugLog("Moving message to inbox: " & _message's subject)
	--				move _message to inbox

This modified script was working really well for me, but now some of the color labeled emails are still in my junk mailbox and not in the trash. I will paste in the script below, but it hasn’t changed and it says move all colors to trash. It started a week or so ago.

– Apple Mail - Reprocesses unread messages in the junk folder

– Last Modified: 13 April 2023

property pMarkSpamMessagesRead : true

property pMarkGoodMessagesRead : false

property pChangeJunkStatus : true

property pColorSpamMessages : true

property pFlagSpamMessages : false

property pUnflagSpamMessages : false

property pMoveBlueMessagesToTrash : true

property pMoveGrayMessagesToTrash : true

property pMovePurpleMessagesToTrash : true

property pMoveRedMessagesToTrash : true

property pMoveOrangeMessagesToTrash : true

property pMoveYellowMessagesToTrash : true

property pEnableDebugLogging : false

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.

my filterServerJunkMailboxes()

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 - Rescue Good Messages” access to control Mail and SpamSieve from System Preferences > Security & Privacy > Privacy > Automation. For more information, please see:

SpamSieve Manual: Security & Privacy Access"

display alert _error message _alertMessage

end if

end try

tell application “Mail”

try

set _mailboxes to mailboxes of junk mailbox

repeat with _mailbox in _mailboxes

try

my filterMailbox(_mailbox)

on error _error number _errorNumber

my logToConsole(“Error " & _errorNumber & " filtering mailbox “” & _mailbox’s name & “” of account “” & _mailbox’s account’s name & "”: " & _error)

end try

end repeat

on error _error

my logToConsole("Error filtering junk mailboxes: " & _error)

end try

end tell

end filterServerJunkMailboxes

on filterMailbox(_mailbox)

tell application “Mail”

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 unprocessed 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 and background color is none

end timeout

my debugLog(my makeLogMessage(“Messages to process in mailbox”, _mailbox, count of _messages))

repeat with _message in _messages

if not my processMessageIfSpam(_message) then

if pMarkGoodMessagesRead then set _message’s read status to true

– my debugLog("Moving message to inbox: " & _message’s subject)

– move _message to inbox

end if

end repeat

my debugLog(my makeLogMessage(“Finished checking mailbox”, _mailbox, “”))

end tell

end filterMailbox

on processMessageIfSpam(_message)

set _source to my sourceFromMessage(_message)

tell application “SpamSieve”

set _score to score message _source without auto training

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 then

set _message’s read status to true

end if

set _moveToTrash to my colorMessageAndDecideIfShouldMoveToTrash(_message, _score)

if _moveToTrash then

delete _message

else if _isSpam then

my debugLog("Leaving spam message in Junk: " & _message’s subject)

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 pUnflagSpamMessages then

set _message’s flag index to -1

end if

if pFlagSpamMessages then

set _message’s flag index to _flagColor

end if

return _moveToTrash

end if

end repeat

end tell

end colorMessageAndDecideIfShouldMoveToTrash

– Logging

on debugLog(_message)

if pEnableDebugLogging then my logToConsole(_message)

end debugLog

on logToConsole(_message)

set _logMessage to "SpamSieve [Apple Mail Rescue Good Messages] " & _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

I suggest that you enable debug logging in the script. Then open the Console app and enter Apple Mail Rescue Good Messages in the search field. When you test the script, it will then tell you more about what it’s doing and why.

Okay, it looks like it only checked one message?

“Messages to process in mailbox “iCloud” / “Junk”: 1|”

That one was moved to trash and marked read as it should be.The other 19 are marked with various colors but were left alone. They are all marked unread, so I don’t know why they aren’t considered unprocessed.

Entire log below.

default 14:13:57.904473-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Start checking mailbox “On My Mac” / “Junk”:
default 14:13:57.953013-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Total messages in mailbox “On My Mac” / “Junk”: 0
default 14:13:58.003864-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Getting unprocessed messages in mailbox “On My Mac” / “Junk”:
default 14:13:58.072269-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Messages to process in mailbox “On My Mac” / “Junk”: 0
default 14:13:58.116962-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Finished checking mailbox “On My Mac” / “Junk”:
default 14:13:58.154671-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Start checking mailbox “iCloud” / “Junk”:
default 14:13:58.207591-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Total messages in mailbox “iCloud” / “Junk”: 20
default 14:13:58.246473-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Getting unprocessed messages in mailbox “iCloud” / “Junk”:
default 14:13:58.310729-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Messages to process in mailbox “iCloud” / “Junk”: 1
default 14:13:58.381146-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Getting source of message in “iCloud” / “Junk”: Get Up to $26,000 per Employee With the ERC Tax Credit!
default 14:13:58.456327-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Spam score of message is 99: Get Up to $26,000 per Employee With the ERC Tax Credit!
default 14:13:58.613362-0600 logger SpamSieve [Apple Mail Rescue Good Messages] Finished checking mailbox “iCloud” / “Junk”:

SpamSieve only treats messages with no background color as unprocessed. Are you using some other software or rule that assigns colors? Or are you for some reason wanting SpamSieve to reprocess messages that it already filtered?

No, these are SpamSieve applied colors. Perhaps it was a glitch. I will see if more stay in there tomorrow.