Mail Account Select Works in Editor; Does Not from AS Menu

The code below works when it is run in the AS editor. It does not work when the code is selected from the AS menu.

The Scene: Cursor at end of new message in Apple Mail. Code is run.

The message ending gets keystroked and the account is selected when run from the editor. Same code run from the Applescript Menu message ending gets keystroked, but account is not selected.

Your insights are eagerly sought.

tell application "Mail" to activate
tell application "System Events"
	set theMsg to return & "Sincerely," & return & return & "Joe Blow" & return
	keystroke theMsg
	tell process "Mail"
		click pop up button 1 of window 1
		click last menu item of menu 1 of pop up button 1 of window 1
	end tell
end tell

Model: Mac Pro (Mid 2010)
Browser: Firefox 79.0
Operating System: macOS 10.14

haolesurferdude. I ran the script multiple times from Script Editor, the AS menu, and FastScripts, and the script worked fine on all occasions. Perhaps this is a timing issue and inserting a few delays in the script might help. I am running Catalina, though, and possibly the macOS difference is in some way responsible.

Thank you very much for the feedback. It’s getting late here, but I will try it tomorrow! Thank you again!

Peavine correctly noted that the instability of your code is most likely due to time delays. I see 3 weak points in the code:

  1. it is better to set the message outside the tell block (because it is plain text)
  2. for code stability, you need to check the fact of the appearance of window 1
  3. also, for the stability of the code, you need to check the fact of the appearance of the menu 1.

Here I am trying to provide you with a complete solution. It implements what I said above. So:


set theMsg to return & "Sincerely," & return & return & "Joe Blow" & return
tell application "Mail" to activate

tell application "System Events"
	repeat until window 1 of application process "Mail" exists
		delay 0.02
	end repeat
	keystroke theMsg
	tell application process "Mail" to tell window 1 to tell pop up button 1
		click it
		repeat until menu 1 exists
			delay 0.02
		end repeat
		click last menu item of menu 1
	end tell
end tell

KniazidisR, thank you very much for taking the time to produce such a thoughtful solution. I am going to school on your solution because of its sophisticated method of approaching GUI scripting.

Thank you again!

HaoleSurferDude

Model: Mac Pro (Mid 2010)
Browser: Firefox 79.0
Operating System: macOS 10.14