AppleScript cannot find flagged email in Trash

I’m trying to write an AppleScript that sets the flagged status of an email in the Trash from true to false. But the script cannot find the emails that have a flagged status of true in the Trash. I don’t receive any errors.

If I change the mailbox to “INBOX” the script will find flagged emails in the Inbox and set the flagged emails to false removing the flag. The account I am having trouble with is an iCloud account. I have two other IMAP accounts and the script works on those accounts. Not sure what I’m doing wrong. Can anybody help? My script is below. Thanks.

tell application “Mail”
repeat with _account in imap accounts
try
set _mailbox to _account’s mailbox “Trash”
set _messages to (a reference to (every message of _mailbox whose flagged status is true))
set _msglist to contents of _messages
repeat with _message in _msglist
–try
set flagged status of _message to false
–end try
end repeat
end try
end repeat
end tell

Hi, and welcome. I fixed some errors. This works for me:


tell application "Mail"
	repeat with _account in imap accounts
		try
			set _mailbox to _account's mailbox "Trash"
			set (flagged status of every message of _mailbox whose flagged status is true) to false
		end try
	end repeat
end tell