Yosemite Mail, creating message with specific from

One again, it appears that Apple has broken scripting in mail. For years, I have had this piece of script running from within FileMaker, but with Yosemite, it’s stopped working.

-- set the reply-to address here
tell application "System Events"
	activate application "Mail"
	tell pop up button "From:" of window 1 of application process "Mail"
		click
		tell menu item "First Last <addr@mail.com>" of menu 1 to click
	end tell
end tell

FileMaker fires off an error that System Events go an error – can’t get menu item.

I don’t care how to do this, and I can do it at creation time of the message, or after the message is created. I just want to be able to set the from address from the list of accounts that I have in Mail.app

For example, I also tried reverting to the old way (which made so much more sense, but doesn’t work either):

 set theFromAccount to "<addr@mail.com>"
 set theNewMessage to make new outgoing message with properties {sender:theFromAccount,visible:true, subject:"subject line", content:"the content is here"}

That doesn’t work either. Any ideas how to do this?

Thanks!

Neil

(1) I never call activate in a tell application “System Events” block.
(2) With Yosemite, activate doesn’t immediately put the corresponding process at front.
At first I added a delay but I’m not sure that the delay sufficient on my machine will be sufficient on an other one.
Setting explicitly the process at front works everywhere.

I am accustomed to use “set frontmost to true”.
In some messages, Nigel Garvey used “set visible to true”.
The result is the same.

Try with :

-- set the reply-to address here
activate application "Mail"
tell application "System Events"
	set frontmost of process application process "Mail" to true
	tell pop up button "From:" of window 1 of application process "Mail"
		click
		tell menu item "First Last <addr@mail.com>" of menu 1 to click
	end tell
end tell

If it works this way, it would be cleaner to code :

 -- set the reply-to address here
activate application "Mail"
tell application "System Events"
	tell application application process "Mail"
		set frontmost to true
		tell pop up button "From:" of window 1
			click
			tell menu item "First Last <addr@mail.com>" of menu 1 to click
		end tell
	end tell
end tell

Yvan KOENIG (VALLAURIS, France) dimanche 8 février 2015 11:16:46

Hi Yvan.

I think ‘frontmost’ is generally better. I suggested ‘visible’ in one case recently where the application was intially hidden and the poster resented its having to come to the front for GUI Scripting. In that case, merely making it temporarily visible turned out to be good enough and shortened the visual distraction on screen.

I struggled with the above, and couldn’t get it to work. Ultimately, this was the ticket to getting it to work. The “sender” format being exactly as shown was important.

Now, since Mail is not a particularly stable app – you need to watch out for running this on larger quantities of records as there’s a decent chance that one of Mail.app’s many memory leaks will cause the app to crash. But I don’t believe this has anything to do with the below script – more Mail.app’s general instability.

tell application “Mail”
set theOutMessage to make new outgoing message with properties {visible:true}
tell theOutMessage
make new to recipient at end of to recipients with properties {address:“first@mail.com”}
set sender to “Name Surname name.surname@mail.com
set subject to “Message Subject”
set content to “Message Text”
end tell
end tell