Scripting to mark all Apple email as read

Hello all — first time post.

I wanted to script Apple Mail to mark all messages in any particular mailbox as read. I have a menu-driven approach that first selects all, and then selects 'Mark as Read.' Works fine as long as the starting message is not read. However, when the starting message is read, the macro cancels because 'Mark as Read' has been replaced by 'Mark as Unread.' As I better first step, I'd like to check the marked status, toggle the Mark menu if required, and then proceed as above. I'm not KM-savvy enough to know how to do that, but I think it could be done either by checking the message or the menu. Don't care at the moment. Any help or better advice appreciated.

Thanks, Mick

OK, answering my own post. This Applescript seems to do it. Clicking 'menu item 10' obviates the issue.

activate application "Mail"
tell application "System Events"
tell process "Mail"
tell menu bar 1
tell menu bar item "Edit" to tell menu 1 to click menu item "Select All"
tell menu bar item "Message" to tell menu 1 to click menu item 10
end tell
end tell
end tell
tell application "Mail"
set theMessages to selection
set theMailBox to mailbox of item 1 of theMessages
set theMessage to first message of theMailBox
set selected messages of first message viewer to theMessage
end tell
tell application "System Events" to key code 126

I have no idea why the last line is required, but without it, the 'first message of theMailBox' is the second one down.