Reply to a Mail message: insert text above signature and quoted text

So I’m trying to use AppleScript to reply to a message in mail, and then insert a line above the text that the reply message has quoted.

Here’s what I have so far:


tell application "Mail"
	tell account "accountName"
		tell mailbox "mailboxName"
			tell messages
				tell item 1
					set replyMessage to reply
				end tell
			end tell
		end tell
	end tell
end tell


tell application "Mail"
	tell replyMessage
		set paragraph 1 to "Thank you!"
	end tell
end tell

The problem is that the actual reply message appears unchanged. In Script Debugger, I see that the text “Thank you!” has been inserted into the first line, so that object model looks like:
replyMessage → paragraphs → paragraph 1 → “Thank you!FirstName LastName”, but in Mail, the first line (paragraph 1) is still:

FirstName LastName

If I set the content class instead of the paragraph class:


set content to "Thank you!"

The text gets inserted into the first line in the Mail reply message, but the signature and quoted text are deleted.

Is there a way to create a reply message, maintain the signature and quoted text, while inserting next text above it?

@Wayland

in the tags, don’t use “appleScript” but “applescript”

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 3 juin 2020 23:26:20

@yvan

Thanks for that. I was wondering about that.

May you try to use

set content to "Thank you" & linefeed & content

or

set paragraph 1 of content to "Thank you" & linefeed & paragraph 1 of content

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 3 juin 2020 23:37:15

Thanks for the assistance, I tried both your lines, but the “Thank you” text does not show up in the actual reply message.

It does show up in Script Debugger’s variable inspector though.

It’s the normal behavior.

I failed to check to check the Mail’s dictionary in which I would have read :
content (rich text, r/o) : Contents of an email message

r/o means : read-only so there is no hope to change its value.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 4 juin 2020 11:25:19

Would be useful to look at : https://macscripter.net/viewtopic.php?id=23737

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 4 juin 2020 12:12:26

Ah, the read only property explains it.

I will try to get around this by first creating a reply message and moving the contents into a variable.

Close the reply message.

Concatenate new text “Thank you!” with the variable and assign it to another variable like newContents.

Create another reply message.

Assign the contents of this new reply message to newContents.

When I have time, I will experiment with this and see if it works.

Here is a workaround tested under 10.13.6.


-- use the free CLI : cliclick

-- Yvan KOENIG (VALLAURIS, France) jeudi 4 juin 2020 15:58:04

my Germaine()

on Germaine()
	tell application "Mail"
		set quoteLevel_loc to localized string "QuoteLevel" from table "Compose"
		set decreaseQuoteLevel_loc to localized string "DecreaseQuoteLevel" from table "Compose"
		set format_loc to localized string "ToolbarComposeFormatBar"
		set _sel to selection
	end tell -- Mail
	set myAnswer to "Thank you." & return & "The text gets inserted into the first line in the Mail reply message, but the signature and quoted text are deleted." & return & "Is there a way to create a reply message, maintain the signature and quoted text, while inserting next text above it?"
	repeat with _msg in _sel
		tell application "Mail" to tell _msg
			set replyMessage to reply
		end tell
		tell application "System Events" to tell process "Mail"
			set frontmost to true
			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 scroll area 1
					-- class of UI elements --> {UI element, scroll bar}
					tell UI element 1
						-- class of UI elements --> {group, group, group, group, table, group}
						-- tell group 1 to get class of UI elements --> {}
						-- tell group 2 to get class of UI elements --> {}
						tell group 3
							--class of UI elements --> {static text}
							tell static text 1
								set {x, y} to its position
								-- put the cursor at the very beg of the message
								tell me to do shell script "/usr/local/bin/cliclick c:" & x & "," & y
								-- type a space & a return character (don't use a linefeed)
								keystroke space & return
								-- triple click to select the entire new line
								tell me to do shell script "/usr/local/bin/cliclick tc:" & x & "," & y
							end tell
						end tell -- group 3
						-- tell group 4 to get class of UI elements --> {}
						-- tell group 5 to get class of UI elements --> {}
					end tell -- UI element 1
				end tell -- scroll area 1
			end tell -- window 1
			-- decrease the quote level
			tell menu bar 1
				-- get name of menu bar items --> {"Apple", "Mail", "Fichier", "Édition", "Présentation", "Boîte aux lettres", "Message", "Format", "Fenêtre", "Aide"}
				tell menu bar item format_loc to tell menu 1
					-- get name of menu items --> {"Afficher les polices", "Afficher les couleurs", missing value, "Listes", "Style", "Alignement", missing value, "Indentation", "Niveau de citation", missing value, "Convertir au format texte"}
					tell menu item quoteLevel_loc to tell menu 1
						-- get name of menu items --> {"Augmenter", "Diminuer"}
						click menu item decreaseQuoteLevel_loc
					end tell -- menu item…
				end tell -- menu bar item…
			end tell -- -- menu bar 1
			-- Now the quote level is OK but the text color is set to white. Why ?????
			-- So trigger the dedicated UI element to reset it to black
			tell window 1
				tell group 1
					-- class of UI elements --> {pop up button, pop up button, color well, color well, group, group, menu button, menu button}
					tell color well 1
						click it
						-- class of UI elements --> {pop over}
						tell pop over 1
							-- class of UI elements --> {button, UI element, UI element}
							-- tell button 1 to get its name
							-- tell UI element 1 to get class of UI elements --> {}
							tell UI element 2
								-- class of UI elements --> {button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button}
								-- count buttons -->120
								click button 12 --> black
							end tell -- UI element 2
						end tell -- pop over 1
					end tell -- color well 1
				end tell -- group 1
			end tell -- window 1
			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 scroll area 1
					-- class of UI elements --> {UI element, scroll bar}
					tell UI element 1
						-- class of UI elements --> {group, group, group, group, table, group}
						-- tell group 1 to get class of UI elements --> {}
						-- tell group 2 to get class of UI elements --> {}
						tell group 3
							--class of UI elements --> {static text}
							tell static text 1
								set {x, y} to its position
								-- put the cursor in the space character at the beg of the message
								tell me to do shell script "/usr/local/bin/cliclick c:" & x + 5 & "," & y + 5
								-- type the answer 
								keystroke myAnswer
							end tell
						end tell -- group 3
						-- tell group 4 to get class of UI elements --> {}
						-- tell group 5 to get class of UI elements --> {}
					end tell -- UI element 1
				end tell -- scroll area 1
			end tell -- window 1
		end tell -- System Events
		
		tell application "Mail" to tell replyMessage
			send -- send the message
		end tell
	end repeat
end Germaine

#=====

As always, numerous instructions used to build the code are kept disabled.

Download Cliclick from www.bluem.net
and install it as : “/usr/local/bin/cliclick”
By default, the folder “Macintosh HD:usr:” is invisible.
but you may open it with :

set p2usr to POSIX file "/usr/"
tell application "Finder" to open p2usr

If I remember well, a subfolder named “local” exists
but you will have to create its subfolder “bin” to get the hierarchy:
“Macintosh HR:usr:local:bin:” where you will store “cliclick”.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 4 juin 2020 15:54:44

Script enhanced. Now the answer inserted is no longer restricted to a single line.

I installed cliclick and ran the script.

I got this error message.

System Events got an error: Can’t get group 1 of window 1 of process “Mail”. Invalid index.

Would be useful to know which OS is active on your side.
Below is my script in which I re-enabled every instructions which were disabled because they aren’t useful to do the job.
They just give informations upon the UI elements available.
I just added one instruction


-- use the free CLI : cliclick

-- Yvan KOENIG (VALLAURIS, France) jeudi 4 juin 2020 15:58:04

my Germaine()

on Germaine()
	tell application "Mail"
		set quoteLevel_loc to localized string "QuoteLevel" from table "Compose"
		set decreaseQuoteLevel_loc to localized string "DecreaseQuoteLevel" from table "Compose"
		set format_loc to localized string "ToolbarComposeFormatBar"
		set _sel to selection
	end tell -- Mail
	set myAnswer to "Thank you." & return & "The text gets inserted into the first line in the Mail reply message, but the signature and quoted text are deleted." & return & "Is there a way to create a reply message, maintain the signature and quoted text, while inserting next text above it?"
	repeat with _msg in _sel
		tell application "Mail" to tell _msg
			set replyMessage to reply
		end tell
		tell application "System Events" to tell process "Mail"
			set frontmost to true
			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}
				set nbElements to count UI elements --> 22  -- ADDED
				repeat with i from 1 to nbElements  -- ADDED
					properties of UI element i  -- ADDED
				end repeat   -- ADDED
				tell scroll area 1
					class of UI elements --> {UI element, scroll bar}
					tell UI element 1
						class of UI elements --> {group, group, group, group, table, group}
						tell group 1 to get class of UI elements --> {}
						tell group 2 to get class of UI elements --> {}
						tell group 3
							--class of UI elements --> {static text}
							tell static text 1
								set {x, y} to its position
								-- put the cursor at the very beg of the message
								tell me to do shell script "/usr/local/bin/cliclick c:" & x & "," & y
								-- type a space & a return character (don't use a linefeed)
								keystroke space & return
								-- triple click to select the entire new line
								tell me to do shell script "/usr/local/bin/cliclick tc:" & x & "," & y
							end tell
						end tell -- group 3
						tell group 4 to get class of UI elements --> {}
						tell group 5 to get class of UI elements --> {}
					end tell -- UI element 1
				end tell -- scroll area 1
			end tell -- window 1
			-- decrease the quote level
			tell menu bar 1
				get name of menu bar items --> {"Apple", "Mail", "Fichier", "Édition", "Présentation", "Boîte aux lettres", "Message", "Format", "Fenêtre", "Aide"}
				tell menu bar item format_loc to tell menu 1
					get name of menu items --> {"Afficher les polices", "Afficher les couleurs", missing value, "Listes", "Style", "Alignement", missing value, "Indentation", "Niveau de citation", missing value, "Convertir au format texte"}
					tell menu item quoteLevel_loc to tell menu 1
						get name of menu items --> {"Augmenter", "Diminuer"}
						click menu item decreaseQuoteLevel_loc
					end tell -- menu item…
				end tell -- menu bar item…
			end tell -- -- menu bar 1
			-- Now the quote level is OK but the text color is set to white. Why ?????
			-- So trigger the dedicated UI element to reset it to black
			tell window 1
				class of UI elements -- ADDED <<<<<<<<<<<<<<<<<<<<
				tell group 1
					class of UI elements --> {pop up button, pop up button, color well, color well, group, group, menu button, menu button}
					tell color well 1
						click it
						class of UI elements --> {pop over}
						tell pop over 1
							class of UI elements --> {button, UI element, UI element}
							tell button 1 to get its name
							tell UI element 1 to get class of UI elements --> {}
							tell UI element 2
								class of UI elements --> {button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button}
								count buttons -->120
								click button 12 --> black
							end tell -- UI element 2
						end tell -- pop over 1
					end tell -- color well 1
				end tell -- group 1
			end tell -- window 1
			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 scroll area 1
					class of UI elements --> {UI element, scroll bar}
					tell UI element 1
						class of UI elements --> {group, group, group, group, table, group}
						tell group 1 to get class of UI elements --> {}
						tell group 2 to get class of UI elements --> {}
						tell group 3
							--class of UI elements --> {static text}
							tell static text 1
								set {x, y} to its position
								-- put the cursor in the space character at the beg of the message
								tell me to do shell script "/usr/local/bin/cliclick c:" & x + 5 & "," & y + 5
								-- type the answer 
								keystroke myAnswer
							end tell
						end tell -- group 3
						tell group 4 to get class of UI elements --> {}
						tell group 5 to get class of UI elements --> {}
					end tell -- UI element 1
				end tell -- scroll area 1
			end tell -- window 1
		end tell -- System Events
		
		tell application "Mail" to tell replyMessage
			send -- send the message
		end tell
	end repeat
end Germaine

#=====

The log history is :

Don't clicl [Open this Scriplet in your Editor:]
Here is a log history

tell application "Mail"
	localized string "QuoteLevel" from table "Compose"
		--> "Niveau de citation"
	localized string "DecreaseQuoteLevel" from table "Compose"
		--> "Diminuer"
	localized string "ToolbarComposeFormatBar"
		--> "Format"
	get selection
		--> {message id 77185 of mailbox "Sent Messages" of account id "8466177C-3C92-4B82-BB40-EA424A398F0B"}
	reply message id 77185 of mailbox "Sent Messages" of account id "8466177C-3C92-4B82-BB40-EA424A398F0B"
		--> outgoing message id 4
end tell
tell application "System Events"
	set frontmost of process "Mail" to true
	get class of every UI element of window 1 of process "Mail"
		--> {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}
	get class of every UI element of scroll area 1 of window 1 of process "Mail"
		--> {UI element, scroll bar, scroll bar}
	get class of every UI element of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, table, group, table, group, group, group, group, group, group, group}
	get class of every UI element of group 1 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {}
	get class of every UI element of group 2 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {}
	get position of static text 1 of group 3 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {456, 333}
end tell
tell current application
	do shell script "/usr/local/bin/cliclick c:456,333"
		--> ""
end tell
tell application "System Events"
	keystroke " 
"
end tell
tell current application
	do shell script "/usr/local/bin/cliclick tc:456,333"
		--> ""
end tell
tell application "System Events"
	get class of every UI element of group 4 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {}
	get class of every UI element of group 5 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {static text}
	get name of every menu bar item of menu bar 1 of process "Mail"
		--> {"Apple", "Mail", "Fichier", "Édition", "Présentation", "Boîte aux lettres", "Message", "Format", "Fenêtre", "Aide"}
	get name of every menu item of menu 1 of menu bar item "Format" of menu bar 1 of process "Mail"
		--> {"Afficher les polices", "Afficher les couleurs", missing value, "Listes", "Style", "Alignement", missing value, "Indentation", "Niveau de citation", missing value, "Convertir au format texte"}
	get name of every menu item of menu 1 of menu item "Niveau de citation" of menu 1 of menu bar item "Format" of menu bar 1 of process "Mail"
		--> {"Augmenter", "Diminuer"}
	click menu item "Diminuer" of menu 1 of menu item "Niveau de citation" of menu 1 of menu bar item "Format" of menu bar 1 of process "Mail"
		--> menu item "Diminuer" of menu "Niveau de citation" of menu item "Niveau de citation" of menu "Format" of menu bar item "Format" of menu bar 1 of application process "Mail"
	get class of every UI element of window 1 of process "Mail"  -- The ADDED instruction
		--> {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}
	get class of every UI element of group 1 of window 1 of process "Mail"
		--> {pop up button, pop up button, color well, color well, group, group, menu button, menu button}
	click color well 1 of group 1 of window 1 of process "Mail"
		--> color well 1 of group 1 of window "Re: [Mailbutler] What can we do for you?" of application process "Mail"
	get class of every UI element of color well 1 of group 1 of window 1 of process "Mail"
		--> {pop over}
	get class of every UI element of pop over 1 of color well 1 of group 1 of window 1 of process "Mail"
		--> {button, UI element, UI element}
	get name of button 1 of pop over 1 of color well 1 of group 1 of window 1 of process "Mail"
		--> "Afficher les couleurs…"
	get class of every UI element of UI element 1 of pop over 1 of color well 1 of group 1 of window 1 of process "Mail"
		--> {}
	get class of every UI element of UI element 2 of pop over 1 of color well 1 of group 1 of window 1 of process "Mail"
		--> {button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button, button}
	count every button of UI element 2 of pop over 1 of color well 1 of group 1 of window 1 of process "Mail"
		--> 120
	click button 12 of UI element 2 of pop over 1 of color well 1 of group 1 of window 1 of process "Mail"
		--> button 12 of UI element 2 of pop over 1 of color well 1 of group 1 of window "Re: [Mailbutler] What can we do for you?" of application process "Mail"
	get class of every UI element of window 1 of process "Mail"
		--> {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}
	get class of every UI element of scroll area 1 of window 1 of process "Mail"
		--> {UI element, scroll bar, scroll bar}
	get class of every UI element of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, table, group, table, group, group, group, group, group, group, group}
	get class of every UI element of group 1 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {}
	get class of every UI element of group 2 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {}
	get position of static text 1 of group 3 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {446, 333}
end tell
tell current application
	do shell script "/usr/local/bin/cliclick c:451,338"
		--> ""
end tell
tell application "System Events"
	keystroke "Thank you.
The text gets inserted into the first line in the Mail reply message, but the signature and quoted text are deleted.
Is there a way to create a reply message, maintain the signature and quoted text, while inserting next text above it?"
	get class of every UI element of group 4 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {static text}
	get class of every UI element of group 5 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {static text}
end tell


May you re-try with this version and post the log history generated on your side ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 9 juin 2020 21:12:19

Added 4 instructions to get more infos about UI Elements existing in the reply window.

I’m on macOS 10.15.5

I ran your script again and got the log from the Log History. See pasted below:


tell application "Mail"
	localized string "QuoteLevel" from table "Compose"
		--> "Quote Level"
	localized string "DecreaseQuoteLevel" from table "Compose"
		--> "Decrease"
	localized string "ToolbarComposeFormatBar"
		--> "Format"
	get selection
		--> {message id 248343 of mailbox "Week 15 Lesson 4" of account id "C2ED53FB-04A4-4C8D-800A-7D2A3DA8D0B9"}
	reply message id 248343 of mailbox "Week 15 Lesson 4" of account id "C2ED53FB-04A4-4C8D-800A-7D2A3DA8D0B9"
		--> outgoing message id 7
end tell
tell application "System Events"
	set frontmost of process "Mail" to true
	get class of every UI element of window 1 of process "Mail"
		--> {static text, text field, button, static text, text field, button, static text, text field, scroll area, pop up button, static text, pop up button, static text, toolbar, button, button, button}
	get class of every UI element of scroll area 1 of window 1 of process "Mail"
		--> {UI element, scroll bar}
	get class of every UI element of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {group, group, group, group, group, group, group}
	get class of every UI element of group 1 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {}
	get class of every UI element of group 2 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {}
	get position of static text 1 of group 3 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {34, 238}
end tell
tell current application
	do shell script "/usr/local/bin/cliclick c:34,238"
		--> ""
end tell
tell application "System Events"
	keystroke " 
"
end tell
tell current application
	do shell script "/usr/local/bin/cliclick tc:34,238"
		--> ""
end tell
tell application "System Events"
	get class of every UI element of group 4 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {}
	get class of every UI element of group 5 of UI element 1 of scroll area 1 of window 1 of process "Mail"
		--> {static text}
	get name of every menu bar item of menu bar 1 of process "Mail"
		--> {"Apple", "Mail", "File", "Edit", "View", "Mailbox", "Message", "Format", "Window", "Help"}
	get name of every menu item of menu 1 of menu bar item "Format" of menu bar 1 of process "Mail"
		--> {"Show Fonts", "Show Colors", missing value, "Lists", "Style", "Alignment", missing value, "Indentation", "Quote Level", missing value, "Make Plain Text"}
	get name of every menu item of menu 1 of menu item "Quote Level" of menu 1 of menu bar item "Format" of menu bar 1 of process "Mail"
		--> {"Increase", "Decrease"}
	click menu item "Decrease" of menu 1 of menu item "Quote Level" of menu 1 of menu bar item "Format" of menu bar 1 of process "Mail"
		--> menu item "Decrease" of menu "Quote Level" of menu item "Quote Level" of menu "Format" of menu bar item "Format" of menu bar 1 of application process "Mail"
	get class of every UI element of window 1 of process "Mail"
		--> {static text, text field, button, static text, text field, button, static text, text field, scroll area, pop up button, static text, pop up button, static text, toolbar, button, button, button}
	get class of every UI element of group 1 of window 1 of process "Mail"
		--> error number -1719 from group 1 of window 1 of process "Mail"
Result:
error "System Events got an error: Can’t get group 1 of window 1 of process \"Mail\". Invalid index." number -1719 from group 1 of window 1 of process "Mail"


Model: MacBook
Browser: Safari 605.1.15
Operating System: macOS 10.14

Thank you.

Your answer is not consistent.
At the beginning you wrote that you are running 10.15.5 and at end you wrote that you run 10.14.

In my late message I added some instructions to get infos about the UI elements available in the message window.
May you run this modified version then,
may you send to my mailbox yvankoenig <at> mac com

  • the exact script which you ran
  • a screenshot of the reply message as it appears on your side.
  • the complete log history.
    The content of the mail window is not the one I get under 10.13.6.

Are you running the standard Mail or are you using an extension like mailbutler or daylite ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 11 juin 2020 01:21:10

For a few minutes, I had access to my grand daughter macBook running Catalina.
I hadn’t time to install cliclick but I was able to see that on this machine the group 1 which you report as unavailable is available.

So I repeat my question :
on your machine, are you using an extension like mailbutler, Daylite or maybe one which I don’t know ? I’m quite sure that the answer is YES.
Not only the standard group item is missing but you have two items which doesn’t exist in standard config : a pop up button, and a static text
→ {static text, text field, button, static text, text field, button, static text, text field, scroll area, pop up button, static text, pop up button, static text, toolbar, button, button, button}
These extraneous items aren’t created by the official Mail application. They are created by an extension about which you wrote absolutely nothing.

On the standard Catalina, the available UI elements are :
→ {static text, text field, button, static text, text field, button, static text, text field, scroll area, pop up button, static text, toolbar, group, button, button, button}

The ordering is different than yours or than mine but it doesn’t matter.
What is different from my machine is due to the fact that I had activated these extraneous items (proposed by the official application):
“AXStaticText”, name:“Cci :”
“AXTextField”, name:“Cci :”
“AXButton”, description:“ajouter des contacts”}
“AXStaticText”, name:“Répondre à :”
“AXTextField”, name:“Répondre à :”
“AXButton”, description:“ajouter des contacts”}

I repeat, on your system you have a standard group missing and two third party objects added.

I apologize but installing such third party item on my grand daughter machine is not an option.

Maybe an other helper will continue to search but for me, GAME OVER.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 11 juin 2020 15:14:33

After searching in my archives, I think that you installed Daylite.app which, at least under High Sierra, remove the standard group element and add the extraneous pop up button and static text elements.

Thanks for the assistance Yvan.

When I reply to a post on MacScripter’s System and click on “include system info in post”, it the operating system selector only goes up to macOS 10.14 → I guess I won’t be using this feature then.

I really do have macOS 10.15.5

I was able to solve the problem without using the reply function. The algorithm I used is:

Mail → Loop through every email in current mailbox
-put email email address, subject, content, and attachment name into lists

Numbers (Email Merge) → Use email address as the key and get the English first name of every recipient put into a list.

Mail → Loop through the email address list and send an email to each email, using data from the first step.

The code looks like this:


# Loop through each message in currently selected mailbox to grab sender's info and put into variables
tell application "Mail"
	set senderContents to {}
	set senderInfos to {}
	set senderAddresses to {}
	set senderSubjects to {}
	set senderAttachmentFileNames to {}
	set mailboxName to name of mailbox of item 1 of (get selection)
	set accountName to name of account of mailbox of item 1 of (get selection)
	
	tell account accountName
		tell mailbox mailboxName
			tell messages
				repeat with messageNumber from (count) to 1 by -1
					tell item messageNumber
						set end of senderContents to content
						set end of senderInfos to sender
						set end of senderAddresses to extract address from senderInfos as string
						set end of senderSubjects to subject
						if mail attachments exists then
							set end of senderAttachmentFileNames to name of item 1 of mail attachments
						else
							set end of senderAttachmentFileNames to "No Attachments"
						end if
					end tell
				end repeat
			end tell
		end tell
	end tell
end tell

# Get English first names from Numbers document and put into list senderNames
set cellHit to {}
set senderNames to {}
repeat with senderAddress in senderAddresses
	tell application "Numbers"
		tell document 1
			repeat with sheetNumber from 1 to 4
				tell sheet sheetNumber
					tell table 1
						if (every cell whose value is senderAddress) is not {} then
							set cellHit to (every cell whose value is senderAddress)
							set sheetIndex to sheetNumber
						end if
					end tell
				end tell
			end repeat
			set rowAddress to address of row of item 1 of cellHit
			tell sheet sheetIndex
				tell table 1
					set end of senderNames to value of first cell of row rowAddress
				end tell
			end tell
		end tell
	end tell
end repeat

#send a message to each sender from above steps
repeat with messageNumber from 1 to count of senderAddresses
	tell application "Mail"
		set signatureContent to content of signature 1
		set newOutGoingMessage to make new outgoing message
		tell newOutGoingMessage
			#set visible to true
			make new to recipient at end of to recipients with properties {address:item messageNumber of senderAddresses}
			set subject to "Re: " & item messageNumber of senderSubjects
			set content to "Thank you " & item messageNumber of senderNames & "!

" & signatureContent & "	
	You wrote: 
	" & item messageNumber of senderContents & "
" & item messageNumber of senderAttachmentFileNames
			#send
		end tell
	end tell
end repeat

I wonder why would Apple’s Mail give us a reply function, but won’t let us edit the contents? What would be the purpose of sending a reply without having write access to a message’s contents? Is this a design flaw or is it intentional?

I consider this problem solved, but if you still want to dive deeper, I will give you more information about the reply window:

The extra pop up UI element is because I have an email signature so when a reply window starts an extra pop up UI element is there to select from different signatures. I removed the signature and it looked more like your system, but there is still no group UI element. I haven’t installed anything like daylite or any add-ons to Mail

I dug into the UI elements in my reply windows and this is what comes up:

in Script Debugger,

I typed:


tell application "System Events"
	get process "Mail"
end tell

and then drilled into the Results & Variables Tab:

For the reply window, there are 17 UI elements.

Here’s what the reply window looks like and screenshot of the 17 UI elements:

https://imgur.com/a/MzmLxev

I apologizes, I never saw the pop up button named “Signature” and its associated text field.
I will create a signature to make complementary tests to see if it’s availability is accompanied by the disparition of the group UI element.

About the command named reply, I can’t answer.
If it was introduced only when GUI scripting was introduced, maybe Apple’s engineers did that to allow us to use a script like mine to do the job.
You must ask this question to them.

I reported this odd behavior several times but it seems that macScripter team is not interested to maintain this feature correctly. It’s why I inserted the correct info in my own signature.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 12 juin 2020 01:17:51

Under High Sierra, when the signature pop up is available there is always the group UI element.
If a signature is used by default and if this signature is inserted before the quoted area, the script may be seriously shortened.
I’m not supposed to have access to my grand daughter macbook but you may test on your side.


-- Yvan KOENIG (VALLAURIS, France) 12 juin 2020 19:58:04

my Germaine()

on Germaine()
	tell application "Mail"
		-- set quoteLevel_loc to localized string "QuoteLevel" from table "Compose"
		-- set decreaseQuoteLevel_loc to localized string "DecreaseQuoteLevel" from table "Compose"
		-- set format_loc to localized string "ToolbarComposeFormatBar"
		set x to "CTM-M5-1Qy.title"
		if (system attribute "sys2") as integer < 15 then
			set signature_loc to localized string x from table "ComposeViewController"
		else
			set signature_loc to localized string x from table "ComposeViewControllerWK2"
		end if
		if signature_loc is x then set signature_loc to "Signature:"
		set x to "100466.title"
		set none_loc to localized string x from table "ViewerPreferences"
		if none_loc is x then set signature_loc to "None"
		
		set _sel to selection
	end tell -- Mail
	
	set myAnswer to "Thank you." & return & "The text gets inserted into the first line in the Mail reply message, but the signature and quoted text are deleted." & return & "Is there a way to create a reply message, maintain the signature and quoted text, while inserting next text above it?"
	
	repeat with _msg in _sel
		tell application "Mail" to tell _msg
			set replyMessage to reply
		end tell
		tell application "System Events" to tell process "Mail"
			set frontmost to true
			tell window 1
				set UIElements to 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, pop up button, static text, button, button, button, toolbar, group}
				name of pop up buttons --> {"De :", "Signature :"}
				value of pop up buttons --> {"Yvan KOENIG – koenigyvan<at>mac<dot>com", "Aucune"}
				if not (exists (pop up button 1 whose name is signature_loc and value is not none_loc)) then error "No signature available" number -128
				(*
				set nbElements to count UIElements
				repeat with i from 1 to nbElements
					properties of UI element i
				end repeat
				*)
				tell scroll area 1
					-- class of UI elements --> {UI element, scroll bar}
					tell UI element 1
						-- class of UI elements --> {group, group, group, group, group, group, group, group, table, group, group}
						tell group 3
							--class of UI elements --> {static text}
							tell static text 1
								keystroke myAnswer & return
							end tell -- static text 1
						end tell -- group 3
					end tell -- UI element 1
				end tell -- scroll area 1
			end tell -- window 1
		end tell -- System Events
		
		tell application "Mail" to tell replyMessage
			-- send -- send the message -- ENABLE it after testing
		end tell
	end repeat
end Germaine

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 12 juin 2020 12:17:21

In fact, at least under High Sierra, the unique requirement is the availability of the pop up “Signature:”.
Even it’s set to None, the short script does its duty.


-- Yvan KOENIG (VALLAURIS, France) 14 juin 2020 16:41:04

my Germaine()

on Germaine()
	tell application "Mail"
	-- Grab the localized spelling only once
		set x to "CTM-M5-1Qy.title"
		if (system attribute "sys2") as integer < 15 then
			set signature_loc to localized string x from table "ComposeViewController"
		else
			set signature_loc to localized string x from table "ComposeViewControllerWK2"
		end if
		if signature_loc is x then set signature_loc to "Signature:"
		set _sel to selection
	end tell -- Mail
	-- Edit the instruction below to fit your needs
	set myAnswer to "Thank you." & return & "The text gets inserted into the first line in the Mail reply message, but the signature and quoted text are deleted." & return & "Is there a way to create a reply message, maintain the signature and quoted text, while inserting next text above it?"
	
	repeat with _msg in _sel
		tell application "Mail" to tell _msg
			set replyMessage to reply
		end tell
		tell application "System Events" to tell process "Mail"
			set frontmost to true
			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, pop up button, static text, button, button, button, toolbar, group}
				-- name of pop up buttons --> {"De :", "Signature :"}
				-- value of pop up buttons --> {"Yvan KOENIG – koenigyvan<at>mac<dot>com", "Aucune"}
				if not (exists (pop up button 1 whose name is signature_loc)) then error "The “" & signature_loc & "” pop up is unavailable" number -128
				tell scroll area 1
					tell UI element 1
						tell group 3
							tell static text 1
								keystroke myAnswer & return
							end tell -- static text 1
						end tell -- group 3
					end tell -- UI element 1
				end tell -- scroll area 1
			end tell -- window 1
		end tell -- System Events
		
		tell application "Mail" to tell replyMessage
			-- send -- send the message -- ENABLE it after testing
		end tell
	end repeat
end Germaine

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 14 juin 2020 16:49:02

Thank you Yvan,

The script from your last post worked on my system.