[FIRST POST] Identify Outgoing Message not created by Applescript

Hello,

I’ve just subscribed, and I’m at my very first steps into the world of automation with Applescript. I’m very excited to become part of this community and I hope, one day, to be a contributor rather than just a beggar :slight_smile:

The first script I’m working on is apparently quite simple, but I’m already running into a couple of troubles.

Context: I have two email addresses. I receive inquires from my commercial website to my personal email (e.g. personal@gmail.com) and when I reply I change my sender address to my company .com email address. I would like to create an Applescript that creates a reply message from the selected message in Mail.app and changes the sender email to the company email address (e.g. email@company.com).

I’ve already succeeded in this with the following script:


set MsgSender to "First Last <email@company.com>"

tell application "Mail"
	set TheMessages to the selection
	repeat with current_Message in TheMessages
		set replyMessage to reply current_Message with opening window and reply to all
		tell replyMessage
			activate
			set sender to MsgSender
		end tell
	end repeat
end tell

What’s the problem with this, then?

The problem is that the new message created by Applescript “reply” command is in a strange format (the original being HTML) and different from the same message created by click on CMD-R in the Mail.app.

I’ve created a screenshot to show you the difference.

The main difference is that the left email has the grey background only on the quoted part; the right-hand side email has grey on ALL the background.

So I’ve tried a different approach. I create the message by sending a keystroke CMD+R to Mail. This generates the email with the correct format that I like (left side of the image above). Now I need to manipulate the outgoing message to change the sender.

How do I send commands to the open window generated by CMD-R, so that I can change the sender?

An additional question: why is the format of the generated email different in the two cases? Shouldn’t it be the same?

Thanks in advance for all your help!

With an added instruction the script behaved flawlessly here, under 10.13.6.

set MsgSender to "First Last <email@company.com>"

tell application "Mail"
	activate -- ADDED
	set TheMessages to the selection
	repeat with current_Message in TheMessages
		set replyMessage to reply current_Message with opening window and reply to all
		tell replyMessage
			activate
			set sender to MsgSender
		end tell
	end repeat
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 13 mars 2020 23:16:55

Hi Yvan,

Thanks! I’ve tried with the added line, but the result is the same. The outgoing email is not formatted as when I hit CMD+R in Mail.app.

Any idea on how to address the outgoing message created with CMD+R with commands from applescript?

Thanks!

PS: my system is 10.15.3 with Mail.app Version 13.0 (3608.60.0.2.5).

I posted a functional script how to format mails, but you’ve to choose font, font size (and color if you want) by yourself
https://macscripter.net/viewtopic.php?id=47198

Of course you could analyze the content of each sender’s mail to instruct your script what formatting to use but that requires some extra work.
Furthermore, the reply command you used is all other than optimal to manipulate mails, and I prefer much more to write mails from scratch.
The property Content gives you simple text only but you can format it

Here, under High Sierra, I see no difference between what is generated by the two schemes.
So I’m not fond of the one using a shortcut because it requires GUI scripting.

I wrote it and tested it.
Here it does the job but I can’t guarantee that you will get the wanted result.

set MsgSender to "First Last <email@company.com>"
set MsgSender to "First Last email@company.com" -- On my machine the character < and > aren't accepted

tell application "Mail"
	activate
	-- grab the localized description of the Send button which has no name
	set send_loc to localized string "ToolbarSend"
	set TheMessages to the selection
	repeat with current_Message in TheMessages
		tell current_Message
			open it -- open the window to be able to issue a shortcut
			tell application "System Events" to tell process "Mail"
				set frontmost to true
				tell window 1
					-- class of UI elements --> {scroll area, button, button, button, toolbar}
					set {{x, y}, {w, h}} to {position, size}
				end tell
				-- click in the window of the message to give it focus
				tell me to do shell script "/usr/local/bin/cliclick c:" & x + w div 2 & "," & (y + h div 2)
				keystroke "R" using {command down, shift down}
				repeat 20 times
					if exists pop up button 1 of window 1 then exit repeat
					delay 0.2
				end repeat
				tell window 1
					-- class of UI elements --> {static text, text field, button, static text, text field, button, static text, text field, button, static text, text field, button, static text, text field, scroll area, pop up button, static text, button, button, button, toolbar, group}
					tell pop up button 1
						click it
						tell menu 1 to click menu item MsgSender
					end tell
					tell toolbar 1 to click (first button whose description is send_loc) -- click the Send button
				end tell -- window 1
			end tell -- System Events …
		end tell --  current_Message
	end repeat
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 14 mars 2020 15:36:13