Mail AppleScript opens viewer just fine but will not jump to Message 1

Attached is my AppleScript that opens the current Message Viewer Window just fine but will not jump back to the INBOX for one of my accounts. The Error Message displayed appears when I try to run it. Many times, I will be in another account when I close the Viewer window, but alway want to be taken back to the newest message in my “INBOX” of account “alf1gm” when I reopen the Viewer window. Any suggestions how to fix this?


on run
	-- set _inbox to mailbox "INBOX" of account "alf1gm"
	
	tell application "Mail"
		activate
		try
			id of message viewer 1
		on error
			make new message viewer
		end try
		--	tell application "System Events" to keystroke "w" using command down
		delay 1
		set _inbox to mailbox "INBOX" of account "alf1gm"
		set theMessage to message 1 of mailbox "INBOX" of account id _inbox
	end tell
	-- ______________________________________________________
	-- error "Mail got an error: Can’t make mailbox \"INBOX\" of account id \"74319E19-B1D0-45DD-8888-69D49A8C7032\" into type rich text." number -1700 from mailbox "INBOX" of account id "74319E19-B1D0-45DD-8888-69D49A8C7032" to rich text

end run


Hi, You probably want to enforce a selection; something like this should work:

tell application "Mail" to set message viewer 1's selected messages to account "alf1gm"s mailbox "INBOX"'s message 1

Marc, Thanks for the reply.

The code addition you suggested does not give an error when compiled, or when run, but it also does not jump to the “alf1gm” INBOX message 1 when the AppleScript is executed.

It simple stays at whichever folder it was initially in when I closed the message viewer window.

Any other suggestions? Is some system event need to be triggered to force the jump to actually be displayed? I can get an automator jump to display the Jump if I add a Recorded “Mouse Over” and “Click” move, but that is clugie at best.

This is not the first time I have come across this question on the net. The answer is coffin urine. I’ll share my solution:


tell application "Mail"
	activate
	
	-- make message viewer 1 if it doesn't exist
	if not (exists message viewer 1) then make new message viewer
	-- message 1 of mailbox "INBOX" of my iCloud account
	set theMessage to message 1 of mailbox "INBOX" of account 1
	-- select the message
	tell message viewer 1 to set selected messages to theMessage
	-- bring message viewer 1 to front, highlight the selected message
	set visible of window of message viewer 1 to true -- important
	
end tell

KniazidisR,
Thank you for the useful reply. I tried to run it as a simple AppleScript, but it did not work either.

However, I embedded the code in an Automator App, Recorded a double point and click after your code to select my Inbox Account, Saved and moved the Automator App to my Applications Folder, authorized the Automator app under "System Preferences…/Security & Privacy/Privacy/Accessibility & Full Disk Access, ran it once so I could click the request for authorization dialog boxes. Then ran it one more time to test that it would work in “FULL AUTONOMOUS” Mode.

Without the above level of diligence, Full Autonomous Automation is no longer possible.

Apple has made full automation of processing steps a royal pain in the butt!!

Support Environments like MacScripter have helped me many times to get the Code Right. But, because folks are busy (I guess), they almost never help the beginner get past the coding part and into full production and use of the code. The paragraph above is my attempt to rectify that, since Apples efforts to secure the Mac OS X environment have made my coding effort a lot more tedious.

Sorry for the Rant, but useful coding should not have become this difficult.

I don’t believe the forum’s intent was ever to supply fully fleshed out solutions for newbies, which might actually prevent you from learning. That said, this is a fairly simple task, and it appears my original advice targeted the wrong inbox. This works:

tell application "Mail"
	activate
	if message viewers = {} then make message viewer
	set message viewer 1's selected mailboxes to inbox
	set message viewer 1's selected messages to account 1's mailbox "INBOX"'s message 1 --*
end tell

*edited for option

Haste is only needed when catching fleas. You definitely substituted your own for my mailbox, or did you try to dig into mine? You should put into the code some real message existing in your Mail.app and not try to use my Mail.app…

I double-checked. Everything works. And remove delay 10 at the end of my script.

Mark Anthony,

Your script doesn’t work if the Mail.app is running but user closed all viewers before running the script.
Last edited by KniazidisR (Today 09:05:58 am)

Please watch your language.

Since the OP’s an obvious beginner and the script they posted contains their own mailbox, it wouldn’t hurt to use that instead of your own in your reply.