Applescript to set the flagged status of emails in the trash (Apple Mail)

Hello,


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. 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. Not sure what I'm doing wrong. Can anybody help? My script is below. Thanks.



tell application "Mail"

repeat with _account in imap accounts

set _inbox to _account's mailbox "Trash"

set _messages to (a reference to (every message of _inbox 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 repeat

end tell

MacBook Pro 15″, macOS 11.0

Posted on Dec 12, 2020 11:04 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 14, 2020 7:22 PM

I figured it out. In the mailbox behavior settings I had "Trash Mailbox" set to "Deleted Messages". Setting "Trash Mailbox" to "Trash" the script started working.

Similar questions

7 replies

Dec 13, 2020 8:09 AM in response to langhamc

Where are you getting errors?

I get an error enumerating all the Trash mailboxes since some accounts don't have a Trash mailbox.

Every account doesn't have a "Trash" mailbox, so you would need to test for that.


You could just wrap it all in a try as you don't need to process accounts without a "Trash" mailbox.

tell application "Mail"
	repeat with _account in imap accounts
		try
			set _inbox to _account's mailbox "Trash"
			set _messages to (a reference to (every message of _inbox 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

Dec 13, 2020 8:31 AM in response to Barney-15E

I do not get errors. Here is some additional info. I have three IMAP accounts an iCloud and two AOL accounts. The script works on the two AOL accounts but not on the iCloud account. The statement "set _messages to (a referenceto (everymessageof _inbox whose flagged status is true))" returns zero flagged email even though there are flagged emails in iCloud's Trash mailbox.

If I set the variable " _inbox " to "INBOX" flagged messages are returned from the Inbox. It appears that you can't get the flag status of emails that are in iCloud's Trash mailbox.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to set the flagged status of emails in the trash (Apple Mail)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.