REPLY TO address in Mail?

I am VERY new to Applescript and have been trying for days to figure out how to set a REPLY TO address in Mail. And now I’m also stuck on formatting a list of BCC recipients. These are the final elements for a script I’m trying to set up for a friend.

This is what I have so far for my test script:

tell application "Mail"
	activate
	set theRecipient to "<you@yourdomain.com>"
	set theBCCRecipients to "them1 <they1@theirdomain.com>," & " them2 <they2@theirdomain.com>," & " them3 <they3@theirdomain.com>" 
		--> this only creates a single (incorrect) addess and nothing else I tried works either
	set theReplyToAddress to "<NOREPLY@mydomain.com>"
	set theSendingAccount to "Me <me@mydomain.com>"
	set theSubject to "This is the Subject"
	set theMessage to "This is text for the body of the message."
	
	set newMessage to (make new outgoing message with properties {visible:true, subject:theSubject, sender:theSendingAccount, content:theMessage})
	
	tell newMessage
		make new to recipient with properties {address:theRecipient}
		make new bcc recipient with properties {address:theBCCRecipients} --> incorrect address list
		set reply to to theReplyToAddress --> can't figure this out
	end tell
	
end tell

Everything works except the REPLY TO and the BCC RECIPIENTS.

I want theBCCRecipients to eventually be populated by a field in Filemaker that is already filled with a list of recipients. I figured out how to extract the field contents but I’m not doing it for this example. What I can’t figure out is how to format the list. The REPLY TO address seems it should be pretty straight-forward but I’ve tried everything I can think of and nothing works.

Can anyone help me?

Thank you,

Vera

Model: G5 Dual 2.5
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi,

an new (outgoing) message has no reply to property.
To create multiple recipients you can use a list and a repeat loop

tell application "Mail"
	activate
	set theRecipient to "<you@yourdomain.com>"
	set theBCCRecipients to {"them1 <they1@theirdomain.com>", "them2 <they2@theirdomain.com>", "them3 <they3@theirdomain.com>"}
	-- set theReplyToAddress to "NOREPLY@mydomain.com"
	set theSendingAccount to "Me <me@mydomain.com>"
	set theSubject to "This is the Subject"
	set theMessage to "This is text for the body of the message."
	
	set newMessage to (make new outgoing message with properties {visible:true, subject:theSubject, sender:theSendingAccount, content:theMessage})
	
	tell newMessage
		make new to recipient at end of to recipients with properties {address:theRecipient}
		repeat with i in theBCCRecipients
			make new bcc recipient at end of bcc recipients with properties {address:i}
		end repeat 
	end tell
end tell

You beat me to it, Stefan! :slight_smile:

I was going to say the same thing, essentially. You can’t set a reply to in an outgoing message.

Oh, drats… I guess that explains it. It seems a little “unfair” when the field is right there like any other (at least if you display it).

But thank you for the answer.

Is there possibly another way to script the REPLY TO – for instance, by setting the clipboard with the REPLY TO address and then pasting it into the Reply To field? I don’t even know where to begin something like that… This is strictly a utilitarian script so it’s fine if I have to first set up the Mail default manually to keep the the Reply To field displayed.

Thanks again,

Vera

Yes, you can use what is called “GUI scripting” for these kinds of tasks. Go into Script Editor and select “open dictionary” from the “File” menu. Look for “System Events” in the list and open it. There you’ll find commands that let you control buttons, text fields, and other controls of a window or application.

GUI scripting is much like programming a macro (if you’ve ever done that in Windows or Office). You have to get it EXACTLY right, but if you do it will work fine.

I’m no expert, I prefer not to do GUI scripting, but there are folks here who are good at it. Post here if you need help!

Thank you!!! I’ll give that a try next and I’ll probably post if I get stuck again.

Well, I spent nearly the entire day trying to figure out how to enter a value in the “reply to” field of a new mail message. I read Apple’s GUI scripting pages, downloaded the UI Element Inspector, searched for posts from other people with similar problems, and experimented until I was beyond exhausted. I don’t think I’m even close to an answer but I’m convinced there has to be a way to do this.

Can anyone help me or direct me to a really good GUI source on the web?

The question is: How can I automate entering an address in the “reply to” address field (not the “account” or “from” field) when it cannot be addressed in the usual way with Applescript?

Thank you in advance!

To get to the “reply to” field, try something like this:

tell application "System Events"
	set myWin to front window of application "Mail"
	set myText to value of (text field 1) of scroll area 1 of front window of application process "Mail"
end tell

That’s how you would get the value. Setting it would work the same way:

tell application "System Events"
	set myWin to front window of application process "Mail"
	set value of (text field 1) of scroll area 1 of myWin to "some person"
end tell

Make sure the “reply to” text field is visible, or this may not work.

Hi,

this should do it with GUI scripting

tell application "Mail"
	activate
	set theRecipient to "<you@yourdomain.com>"
	set theBCCRecipients to {"them1 <they1@theirdomain.com>", "them2 <they2@theirdomain.com>", "them3 <they3@theirdomain.com>"}
	set theReplyToAddress to "NOREPLY@mydomain.com"
	set theSendingAccount to "Me <me@mydomain.com>"
	set theSubject to "This is the Subject"
	set theMessage to "This is text for the body of the message."
	
	set newMessage to (make new outgoing message with properties {visible:true, subject:theSubject, sender:theSendingAccount, content:theMessage})
	tell application "System Events"
		tell process "Mail"
			tell menu item 9 of menu 1 of menu bar item 5 of menu bar 1
				if value of attribute "AXMenuItemMarkChar" is "" then pick it -- if reply field is not enabled, enable it
			end tell
			set value of text field 1 of scroll area 1 of window 1 to theReplyToAddress
		end tell
	end tell
	tell newMessage
		make new to recipient at end of to recipients with properties {address:theRecipient}
		repeat with i in theBCCRecipients
			make new bcc recipient at end of bcc recipients with properties {address:i}
		end repeat
	end tell
end tell

Wow, this is perfect – THANK YOU both so much!!! I’m sure I could have been at this all MONTH and still not figured that out. I only wish I had given up sooner. ;o)

Both methods worked but the one that displays the REPLY TO field will probably be safer in my situation since the friend I’m setting this up for may not leave the field displayed. I tinkered a little with what you supplied and ALMOST worked out a way to also change the mark back to whatever it was before the script ran. But that isn’t important at all so I will leave it for when I have more time and energy.

One question (for furture reference): How did you determine that it was “text field 1” and “scroll area 1”? (I didn’t see anything like that in Apple’s UI Element Inspector.)

Thanks again!

Vera

Hi Vera,

UIElementInspector is good, but not very detailed.
I use Prefab’s UI Browser, which can determine single elements (there’s also a 30-days trial version)
and Script Debugger, a professional tool to develop scripts, which can display all variables and the hierarchy of UI Elements

In Apple’s software, almost all text fields are encased in scroll areas. Not obvious to the average person, but when you’ve worked in Applescript Studio, you realize this very quickly. Also, the UI Inspector will show you the scroll area if you move the pointer to the very top edge of a text field.

As for how I determine which number an item is - trial and error. I opened a new message and put the numbers 1 through 5 in all the text fields (one in each box). Then I used the “get contents” script to fetch items one at a time until I got the value I was looking for.

There are other, more elegant ways of doing these things, but I usually rely on “brute force” methods because they are a) cheap (no extra software required) and b) they are faster (for me because I’m used to doing it).

You’ve both been a great help and it’s very much appreciated.

Stefan: I’ve looked into your program recommendations and will probably splurge once I’m sure I’ll be doing enough scripting to be worth it. They seem to be the top 2 utilities.

Kevin: Your tips for managing without them will be very useful in the meantime.

As time permits, I’ll also be slowly working my way through (and jumping around within) “AppleScript: The Definitive Guide” by Matt Neuberg. Some things are clicking, others aren’t, but it’s still very helpful. I only wish the definitions in the AS program libraries were better. Those really trip me up but I still managed to think up another completely unrelated script today for the system I’m setting up for my friend, and I actually got it working in less than an hour this time. Is it luck or is it progress? I’ll settle for either one! ;o)

Thanks,

Vera

I know this is an ancient topic but it’s the only one I’ve found after thorough searching that gives the exact solution to the problem I’m having, namely to include a ‘ReplyTo’ address in an email sent out with Applescript. That’s the good news.

The bad news is that it doesn’t work. The solutions posted by Kevin Bradley and StefanK fail at the instructions

 set value of text field 1 of scroll area 1 of window 1 to theReplyToAddress

and

 set value of (text field 1) of scroll area 1 of myWin to theReplyToAddress

respectively. (Sorry, no idea why the code tags aren’t working.)

The first gives ‘System Events error: can’t get text field 1 of scroll area 1 of Process “Mail”. Invalid index’.
The second gives ‘System Events error: can’t get text field 1 of scroll area 1 of window “This is the Subject”. Invalid index’.

The draft email displayed at the end has all fields filled in except ‘To’ and ‘Reply To’ (I’ve commented out all the BCC code, not required).

I have to assume that both these lines worked thirteen years ago as the authors obviously must have tested them before posting and the OP seemed ecstatic, so what has changed in the interim? Can some kind expert check this out for me and possibly provide an up-to-date working revision (preferably the Bradley version)?

I’m totally new to Applescript and am trying to update the script in Ron de Bruin’s code & script for sending emails with attachments from Mac Excel VBA to include a ReplyTo address. I’m an experienced VBA programmer but Mac GUI scripting is pretty opaque to me.

I’m using a Macbook Pro under Catalina (10.15.4).

I hope someone can help as I really need to get this to work. I’m converting a Windows Excel VBA system to work on a Mac, and a ReplyTo can be easily added in the Windows version.

Bill

I can’t guarantee that Mail behaves the same under Catalina but, under High Sierra it doesn’t behave as it did when StefanK wrote its script.
Under 10.13.6, the text field is no longer in a scroll area.
Below is an edited version.

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

-- set fixLocaleShort to current application's NSLocale's currentLocale()'s languageCode() as text
set liveLocaleShort to current application's NSLocale's autoupdatingCurrentLocale()'s languageCode() as text
set sys2 to (system attribute "sys2") as integer
tell application "Mail"
	activate
	
	set x to "fGb-Sh-fUL.title"
	set loc to localized string x from table "ComposeWindowController"
	if (loc = "fGb-Sh-fUL.title") and liveLocaleShort = "en" then
		set loc to "Reply-To Address Field"
	else if liveLocaleShort = "fr" then
		set loc to "Champ Répondre à"
	end if
	set replyToAddressField_loc to loc
	
	set x to "KG0-Fs-IxZ.title"
	if sys2 < 15 then
		set loc to localized string x from table "ComposeViewController"
	else
		set loc to localized string x from table "ComposeViewControllerWK2"
	end if
	if (loc = "KG0-Fs-IxZ.title") and (sys2 < 14) and liveLocaleShort = "en" then
		set loc to "Reply To:" -- for 10.13.6
	end if
	set reply_loc to loc
	
	set theRecipient to "<you@yourdomain.com>"
	set theBCCRecipients to {"them1 <they1@theirdomain.com>", "them2 <they2@theirdomain.com>", "them3 <they3@theirdomain.com>"}
	set theReplyToAddress to "NOREPLY@mydomain.com"
	set theSendingAccount to "Me <me@mydomain.com>"
	set theSubject to "This is the Subject"
	set theMessage to "This is text for the body of the message."
	
	set newMessage to (make new outgoing message with properties {visible:true, subject:theSubject, sender:theSendingAccount, content:theMessage})
	tell application "System Events"
		tell process "Mail"
			tell menu item replyToAddressField_loc of menu 1 of menu bar item 5 of menu bar 1 -- EDITED
				if value of attribute "AXMenuItemMarkChar" is "" then pick it -- if reply field is not enabled, enable it
			end tell
			set value of text field reply_loc of window 1 to theReplyToAddress -- EDITED
		end tell
	end tell
	tell newMessage
		make new to recipient at end of to recipients with properties {address:theRecipient}
		repeat with i in theBCCRecipients
			make new bcc recipient at end of bcc recipients with properties {address:i}
		end repeat
	end tell
	
end tell

You tried to use tags with the strings “code” and “/code” but on this site they are supposed to be “applescript” and “/applescript”
No need to type them by hand, just use the dedicated button, the one between “Quote” and “Center”.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 30 mai 2020 15:32:35

Edited because:
The wanted menu item “Reply-To Address Field” is no longer the one at index 9 but the one at index 13.
The field “Reply To:” is not guaranteed to be at index 3 (on my machine, it’s at index 4)

Added a script listing the localized versions of the string “Reply-To Address Field”

----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

-- Build an array of the localized strings for a given key
-- Yvan KOENIG (VALLAURIS, France) Samedi 30mai 2020
----------------------------------------------------------------

property |⌘| : a reference to current application

set theLanguages to paragraphs of "ar
ca
cs
da
de
el
en_AU
en_GB
en
es_419
es
fi
fr_CA
fr
he
hi
hr
hu
id
it
ja
ko
ms
nl
no
pl
pt_PT
pt
ro
ru
sk
sv
th
tr
uk
vi
zh_CN
zh_HK
zh_TW
Dutch
English
French
German
Italian
Japanese
Spanish"

set root to (path to application "Mail" as text) & "Contents:Resources:"-- EDITED, the app's location changed

set fullTable to "ComposeWindowController.strings"

set theKey to "fGb-Sh-fUL.title"

-- Now extract the value for every language available
set theArray to |⌘|'s NSMutableArray's new()
repeat with usedLanguage in theLanguages
	set theFile to root & usedLanguage & ".lproj:" & fullTable
	set anURL to (|⌘|'s NSURL's fileURLWithPath:(POSIX path of theFile))
	set theDict to (|⌘|'s class "NSDictionary"'s dictionaryWithContentsOfURL:anURL)
	if theDict is not missing value then
		set theValue to (theDict's valueForKey:theKey) as string
	else
		set theValue to "????"
	end if
	(theArray's addObject:((usedLanguage as string) & tab & theValue))
end repeat

set theData to (theArray's componentsJoinedByString:linefeed)

-- save data to new file
set bareName to theKey & " locales"
set hfsPath to (path to desktop as text) & bareName & ".txt"
set targetFile to hfsPath as «class furl»
(theData's writeToURL:targetFile atomically:true encoding:(|⌘|'s NSUTF8StringEncoding) |error|:(missing value))


set wName to bareName & ".txt"
tell application "TextEdit"
	activate
	if exists window wName then close window wName
	open targetFile
end tell

#=====

and what is returned under 10.13.6

ar حقل عنوان رد على
ca Camp d’adreça “Respondre a”
cs Pole Odpověď na
da Adressefeltet Svar til
de Adressfeld „Antwort an“
el Πεδίο διευθύνσεων απάντησης προς
en_AU ???
en_GB ???
en ???
es_419 Campo de dirección “Responder a”
es Campo de dirección “Responder a”
fi Vastaus-osoitekenttä
fr_CA ???
fr Champ Répondre à
he שדה כתובת ״כתובת למענה״
hi उत्तर-प्रति पता फ़ील्ड
hr Adresno polje odgovora
hu Válaszcímmező
id Bidang Alamat Balas-Ke
it Campo indirizzo Rispondi a
ja 返信先アドレス欄
ko 답장 받는 사람 주소 필드
ms Medan Alamat Balas Kepada
nl Antwoord aan-adresveld
no Svar til-adressefelt
pl Pole adresowe Odpowiedź do
pt_PT Campo Responder a
pt Campo de Endereço “Responder A”
ro Câmp de adresă Răspuns către
ru Поле «Обратный адрес»
sk Pole adresáta odpovede
sv Adressfältet Svara till
th ช่องที่อยู่ตอบกลับ
tr Yanıt Adresi Alanı
uk Поле «Кому відповісти»
vi Trường địa chỉ trả lời
zh_CN “回复至”地址栏
zh_HK ???
zh_TW 回覆給位址欄位
Dutch ???
English ???
French ???
German ???
Italian ???
Japanese ???
Spanish ???

If it’s ran under Mojave or Catalina it will return:
ar حقل عنوان رد على
ca Camp d’adreça “Respondre a”
cs Pole Odpověď na
da Adressefeltet Svar til
de Adressfeld „Antwort an“
el Πεδίο διευθύνσεων απάντησης προς
en_AU Reply-To Address Field
en_GB Reply-To Address Field
en ???
es_419 Campo de dirección “Responder a”
es Campo de dirección “Responder a”
fi Vastaus-osoitekenttä
fr_CA Champ Répondre à
fr Champ Répondre à
he שדה כתובת ״כתובת למענה״
hi जवाब-प्रति पता फ़ील्ड
hr Adresno polje odgovora
hu Válaszcímmező
id Bidang Alamat Balas-Ke
it Campo indirizzo Rispondi a
ja 返信先アドレス欄
ko 답장 받는 사람 주소 필드
ms Medan Alamat Balas-Kepada
nl Antwoord aan-adresveld
no Svar til-adressefelt
pl Pole adresowe Odpowiedź do
pt_PT Campo Responder a
pt Campo de Endereço “Responder A”
ro Câmp de adresă Răspuns către
ru Поле «Обратный адрес»
sk Pole adresáta odpovede
sv Adressfältet Svara till
th ช่องที่อยู่ตอบกลับ
tr Yanıt Adresi Alanı
uk Поле «Кому відповісти»
vi Trường địa chỉ trả lời
zh_CN “回复至”地址栏
zh_HK 回覆地址欄位
zh_TW 回覆給地址欄位
Dutch ???
English ???
French ???
German ???
Italian ???
Japanese ???
Spanish ???

Edited the first script because Catalina doesn’t use the same table than older systems

M. Koenig - thank you very much, that worked but with a further little tweak I had to make.

The draft email produced actually had the ReplyTo address in the To field as well as the To address, so I guessed it wasn’t text field 1 of window 1 that should be used for the ReplyTo address any more but, after trying text field 2, I found that text field 3 is now the one to use. TF1 is ‘To’, TF2 is ‘CC’ and TF3 is ‘ReplyTo’. I tried text field 4 but nothing came out.

The email is now being created perfectly and I thank you again for your help.

I apologise for using the tags but in my defence that’s what’s specified for displaying code in the Help page brought up by clicking on the ‘BBCode’ link under the composition area where all the tags to be used for anything are specified. Maybe it’s a little out of date. I had read the Posting Guidelines but that was yesterday and that’s another country sometimes these days.

I think I can declare this problem Solved.

Bill

I carefully wrote :

I apologizes, but as I’m not a sooth sayer, I see only what is displayed on my machine and it’s :

tell application "System Events" to tell process "Mail"
	set frontmost to true
	tell window 1
		get 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}
		name of text fields --> {"To:", "Cc:", "Bcc:", "Reply To:", "Subject:"}
		name of text field 1 --> "To:"
		name of text field 2 --> "Cc:"
		name of text field 3 --> "Bcc:"
		name of text field 4 --> "Reply To:"
		name of text field 5 --> "Subject:"
	end tell
end tell

So, in fact I would have to write text item 4.
It’s always the same kind of problem when we use GUI scripting.
The contents of windows may be different according to user’s settings.
To get rid of that, I edited message #15 so that it no longer trigger the item by its index but by its name.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) Saturday 30 May 2020 18:32:50

I apologise, I wasn’t trying to be picky, only reporting what I found. I did understand your caveat.

I ran your Scriplet and it gave an error:

error “System Events got an error: Can’t get text field 1 of window 1 of process "Mail". Invalid index.” number -1719 from text field 1 of window 1 of process “Mail”.

I commented that line out and it gave the same error for ‘text field 2…’

I commented out all five ‘name of text field n’ lines, leaving only ‘name of text fields’. It ran for a second or two and produced {} in the Result area.

Message #15 still looks the same as it was, after several refreshes, I’m afraid. Maybe you could just provide the new line in here?

Thanks for the time you are spending on this.

Bill

Which scriplet returned such errors ?
If as I assume it’s the one in message #17, it’s logical.
This short test assume that there is a new message window available.
Message #15 was not edited because I discovered an other anomaly which you missed.
The original code was supposed to check the menu item 9 of menu bar 5 so that the “Reply To:” field was available.
Alas, under “recent” systems, it’s no longer at index 9 but at index 13.
Worse, under 10.13.6, the localized name of this menu item is not reachable for at least 9 languages. To be able to post quite quickly, I treated only English and French.
I will complete that soon.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 30 mai 2020 19:30:56

Correct, it was the script in message #17. I think there must have been some sort of timing issue yesterday as I can now see that #15 is up to date with your edited version.

You might like to know that when I ran your script in #15 to list the localised versions of the string “Reply-To Address Field”, every language came out with “???”.

I incorporated the method you added to the original script in #15 to extract the right locations into my emailing testing script and made the corresponding edits required to the email construction code. It gave me this:

“System Events got an error: Can’t set text field "KG0-Fs-IxZ.title" of window 1 of process "Mail" to "(my reply to address)".” number -10006 from text field “KG0-Fs-IxZ.title” of window 1 of process “Mail”

at the line
set value of text field reply_loc of window 1 to theReplyToAddress – EDITED

BTW, my OS got updated last night to 10.15.5.