set email to messages in selected mailbox whose read status is false

Whats wrong with this?

tell application “Mail”
set email to messages in selected mailbox whose read status is false
set read status of myMessages to true
end tell

Hi cirno,

In Mail.app’s Applescript Dictionary ‘selection’ is a reference to a list of messages, not a mailbox.

In the latest version of Mail.app this should work:

tell application "Mail"
	set myMailbox to mailbox of (item 1 of (get selection))
	set read status of every message of myMailbox to true
end tell

I can’t remember when it changed, but I’m pretty sure that in 10.4.1 and earlier ‘mailbox of’ was broken (returning ‘missing value’). I use this way around that problem:

tell application "Mail"
	set myMailbox to my getMailbox(item 1 of (get selection))
	set read status of every message of myMailbox to true
end tell

-- Handler to get mailbox of message. 
on getMailbox(myEmail)
	tell application "Mail"
		try
			set myBox to (mailbox of myEmail)
			myBox
		on error
			try
				get UnassignedVariable of myEmail
			on error theErr
				set AppleScript's text item delimiters to {"\""}
				if (count of text items of theErr) is 5 then
					set myBox to mailbox (text item 2 of theErr) of account (text item 4 of theErr)
				else
					set myBox to mailbox (text item 2 of theErr)
				end if
				set AppleScript's text item delimiters to {""}
			end try
		end try
		return myBox
	end tell
end getMailbox

Best wishes

John M

Only problem with this script is that i need to click to messagelist on right first. It would be easier if i could just click folder list in left and then launch script.

Thanks

Hi cirno,

Well, If you can’t be bothered to click a message :wink:

Try this:

tell application "Mail"
	activate -- Mail.app needs to be active for the keystroke command to work.
	tell application "System Events" to keystroke (ASCII character 31) -- Down arrow
	delay 0.1
	set myMailbox to mailbox of (item 1 of (get selection))
	set read status of every message of myMailbox to true
end tell

John M