Mail Rule Doesn't Fire Up

The rule in question is set to apply to junk mail and execute the following script:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			display notification length of theMessages & " junk messages in the Junk." with title "Handler fired up." subtitle "Rule: " & theRule & "."
			tell junk mailbox
				set JunkMessages to (get every message)
				if theMessages contains some item of JunkMessages then
					if length of JunkMessages > 9 then
						delete every message
					end if
				end if
			end tell
			tell every account to set move deleted messages to trash to true
		end tell
	end perform mail action with messages
end using terms from

I added the notification part to get a visual acknowledgment the handler is running. However, nothing would put it into action. Clicking on Apply Rules would mark some messages as junk letting me believe Mail waits for its designated junk messages to accumulate to more than 9 of those as defined in the code. With that in mind I marked all the messages in the Junk box as junk but trying to apply my rules didn’t pay off. I don’t even get the notification preceding the handler’s statements.

Why?

Hi. The display and length commands in your feedback can’t be realized together, and they need parenthetical prioritization. There’s also no coercion to text, which is necessary, due to the leftmost item being a number. You’ll likely need an explicit get to realize the list to extract some item. I think this will do what you want:

using terms from application "Mail"
	on perform mail action with messages theMessages --for rule theRule —referencing theRule has caused problems in prior scripts, so i avoid it
		display notification (((count theMessages) & " junk messages in the Junk.") as rich text) with title "Handler fired up." —functionally superfluous
		if ((count junk mailbox's messages) > 9) and ((get junk mailbox's messages)'s some item) is in theMessages then move junk mailbox's messages to trash mailbox
	end perform mail action with messages
end using terms from

In continuation of the topic, I have experimented a lot with this mail handler and found that the top-level junk mailbox sometimes accrues messages that aren’t marked as junk. In this case, the corresponding Mail rule from the Mail preferences passes over. However, when I mark those messages as junk explicitly it kicks off.
The most peculiar behaviour I’ve run into is that choosing Filter junk mail before applying my rules renders the handler non-operational.

· We have a mail rule running idle on Junk mailbox if the messages it contains aren’t marked as junk, and the rule null and void when the in-built junk filtering is on. Is it a bug, or a feature?

· What if I need the junk messages moved to Junk and then processed by its targeting rule?

The script in this thread should count anything living in the junk folder, but the rule condition might be triggering by class—i.e. “message is junk mail”—and it could be adjusted to be more broad. Automatic filtering is haphazard, and I see no documentation describing what “filter junk mail before applying my rules” actually means; maybe the intent is to mask detection for non-junk, but you’re looking for a positive, so I’d un-check that option.

The rule is exactly message is junk mail and the pre-filtering is unchecked.