How to apply rules to server junk mail

I am trying to figure out a way to mark junk emails that have been flagged on the server side as junk so that the apple email app sees them as junk. The problem? Messages flagged as junk on the server side do not show “Mail thinks this message is Junk” when viewed on the app and as a result the app shows all remote content (thus confirming receipt to the spammer), unlike mail that is flagged as junk on the email app.

I have tried the following AppleScript but it does not appear to be working, or there is some other flag that is being set to trigger the warning. Any ideas?

on idle
	-- This is executed periodically when the script is run as a stay-open application.
	my deleteSpam()
	return 60 -- Run every minute 
end idle

on deleteSpam()
	tell application "Mail"
		check for new mail
		--
		-- grab all junk messages
		--
		set myJunk to every message of junk mailbox
		repeat with msg in myJunk
			--
			-- Flag server junk messages as junk (to suppress showing images)
			--
			set msg's junk mail status to true
		end repeat
	end tell
end deleteSpam

This works for me. Your’s gave me compile errors due to bad quotes

on deleteSpam()
	tell application "Mail"
		check for new mail
		set myJunk to every message of junk mailbox
		repeat with msg in myJunk
			set msg's junk mail status to true
		end repeat
	end tell
end deleteSpam

This is the same as my script - the compile errors are simply due to my cut and paste into the original post. This runs but does nothing to change the apple mail app behavior - at least for me.

Weird!

It worked for me, but I’m on Monterey.
I’ll try it later on my work Mac on Ventura

Thanks for checking - yes I’m on Ventura and that may be part of the issue. I’ve put this workaround in place that hopefully will do the job - have to wait for more spam to arrive (that’s a weird thing to wish for!).

on deleteSpam()
	tell application "Mail"
		check for new mail
		--
		-- grab all junk messages
		--
		set myJunk to every message of junk mailbox
		repeat with junkMsg in myJunk
			--
			-- Move all server caught spam messages to separate junk folder
			-- 
			if junkMsg's source contains "X-Apple-Movetofolder: Junk" then
				set junkMsg's read status to true
				set junkMsg's junk mail status to true
				move junkMsg to mailbox "Junk/Server_Spam"
			end if
		end repeat
	end tell
end deleteSpam

Just for fun, I modified the script to wait while “Mail” app is getting mail

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
use framework "ScriptingBridge"

deleteSpam()

on deleteSpam()
	local theApp, bac
	tell application "Mail" to check for new mail
	set bac to 1
	repeat until bac = 0 -- wait till done getting mail
		delay 0.2
		set theApp to current application's SBApplication's applicationWithBundleIdentifier:"com.apple.Mail"
		set bac to (theApp's backgroundActivityCount) as integer
	end repeat
	tell application "Mail"
		set myJunk to every message of junk mailbox
		repeat with msg in myJunk
			--set msg's junk mail status to true
			beep
			delay 0.3
		end repeat
	end tell
end deleteSpam

I have something like this as well - was not pasting the whole script - this is the header. Save as application

on idle
	-- This is executed periodically when the script is run as a stay-open application.
	my deleteSpam()
	return 60 -- Run every minute 
end idle