Mouse click on Send button new outgoing mail message

Hello guys,
I need to click by mouse on the Send button new outgoing mail message.
Please help me how to find coordinates of the new outgoing message window,then Send button and click on it by mouse.
Any help will be appreciate.

Lilya

You may try with :

tell application id "com.apple.Mail"
	--properties of every window
	# Get the localized name of the send button
	set sendButton to localized string "ToolbarSend"
end tell
tell application "System Events" to tell process "Mail"
	set frontmost to true
	--title of window 1
	--> "Re: Additional txt here to add before file name"
	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 toolbar 1
			--class of UI elements
			--> {{button, menu button, button, button, button, button, button, button}}
			click (first button whose description = sendButton)
		end tell
	end tell
end tell

or, if you aren’t sure that the window of the mail is not at front try :

set myObject to "Re: Additional txt here to add before file name" # Edit to fit your needs

tell application id "com.apple.Mail"
	--properties of every window
	# Get the localized name of the send button
	set sendButton to localized string "ToolbarSend"
end tell
tell application "System Events" to tell process "Mail"
	set frontmost to true
	--title of window 1
	--> "Re: Additional txt here to add before file name"
	tell (first window whose title is myObject)
		--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 toolbar 1
			--class of UI elements
			--> {{button, menu button, button, button, button, button, button, button}}
			click (first button whose description = sendButton)
		end tell
	end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 30 mars 2019 14:15:16

My recommendation is not to bother. It’ll be a brittle script that’ll likely fail upon slight changes to the environment, for seemingly not much payout, assuming the only part you’re automating of the new outgoing message is the action to send it. If you’ve automated other parts of the new outgoing message, such as its content, recipients, subject, etc., then my new recommendation would be to script the Mail.app, which I believe has commands that allow one to generate and adjust the properties of a new outgoing message; and then to send it. Generally, an example script could look like this:

tell application "Mail" to tell (make new outgoing message with properties ¬
		{subject:theSubject, content:theContent, visible:true, sender:theSender})

		make new to recipient at end of to recipients ¬
			with properties {address:theAddress}

		tell its content to make new attachment at after the last paragraph ¬
			with properties {file name:theAttachmentFile}

		send
end tell

(where all identifiers starting with “the” (e.g. “theSender”) refer to variables you will have set in the preceding lines, assigning appropriate values.)

An alternative to Yvan’s approach might be to target the “Send” menu item under the “Message” menu bar item, checking first it exists (which it will if the outgoing message window is the active one in Mail.app). The benefit of this over targetting the toolbar is that a menu item will likely not change, whereas a toolbar can. The drawback is that the menu item will only exist if the message window is active, whereas a toolbar item is attached to the window, and exists whenever the window exists.

I searched with no result if there is a way to use this edited script:

tell application "Mail" to tell (make new outgoing message with properties ¬
	{subject:theSubject, content:theContent, visible:true, sender:theSender})
	
	make new to recipient at end of to recipients ¬
		with properties {address:theAddress}
	
	tell its content to make new attachment at after the last paragraph ¬
		with properties {file name:theAttachmentFile}
	
	--send # DISABLED
end tell

make some changes in the message then run a new script

tell application "Mail"
	# retrieve the outgoing message created by the first scrip
	send
end tell

which is, if I guess correctly what is asked for.

So I designed an alternate scheme.
The second script ask the user to select the subject of the mail to send in the list of opened windows. Then it brings the window of the mail to front and at last click the menu item “Send”.

tell application "Mail"
	set theWindows to name of windows
	-->  {"Boîte de réception — iCloud (336 messages, 1 non lus)", "New Auctions in your favorite Invaluable categories — Boîte de réception", "new subject"}
	if theWindows = {} then error number -128 # Cancel
	set theWindow to choose from list theWindows with title "Select the subject of the message to send"
end tell # "Mail"

if theWindow is false then
	error number -128 # Cancel
else
	set theWindow to theWindow's item 1 --> "new subject"
end if

tell application "System Events" to tell process "Mail"
	set frontmost to true
	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 -2 to tell menu 1 # Window - Fenêtre
			-- get name of menu items
			--> {"Placer dans le Dock", "Placer toutes les fenêtres dans le Dock", "Réduire/agrandir", "Réduire/agrandir toutes les fenêtres", missing value, "Afficher l’onglet précédent", "Afficher l’onglet suivant", "Placer l’onglet dans une nouvelle fenêtre", "Fusionner toutes les fenêtres", missing value, "Fenêtre de message", missing value, "Navigateur de photos", "Destinataires précédents", missing value, "Activité", "Diagnostic de connexion", missing value, "Tout ramener au premier plan", "Mettre au premier plan", missing value, "Boîte de réception — iCloud (336 messages, 1 non lus)", "New Auctions in your favorite Invaluable categories — Boîte de réception", "new subject"}
			click menu item theWindow
		end tell # menu bar item -2 to tell menu 1
		
		tell menu bar item -4 to tell menu 1 # Message
			--name of menu items
			--> {"Envoyer", missing value, "Répondre", "Répondre à tous", "Réexpédier", "Réexpédier en tant que pièce jointe", "Réexpédier le message et son parent en tant que pièce jointe", "Rediriger", missing value, "Marquer comme lu", "Déplacer vers Indésirables", "Drapeau", "Définir la priorité", missing value, "Archiver", "Déplacer vers la boîte aux lettres prédite", "Déplacer vers", "Copier dans", "Déplacer à nouveau", missing value, "Appliquer les règles", "Ajouter le destinataire aux contacts", missing value, "Supprimer les pièces jointes"}
			click menu item 1 # Send
		end tell #  menu bar item -4
	end tell # menu bar 1
end tell

or, If we choose to trigger the Send button in the toolbar:

tell application id "com.apple.Mail"
	# Get the localized name of the send button
	set sendButton to localized string "ToolbarSend"
	
	set theWindows to name of windows
	-->  {"Boîte de réception — iCloud (336 messages, 1 non lus)", "New Auctions in your favorite Invaluable categories — Boîte de réception", "new subject"}
	if theWindows = {} then error number -128 # Cancel
	set theWindow to choose from list theWindows with title "Select the subject of the message to send"
end tell # "Mail"

if theWindow is false then
	error number -128 # Cancel
else
	set theWindow to theWindow's item 1 --> "new subject"
end if

tell application "System Events" to tell process "Mail"
	set frontmost to true
	tell menu bar 1
		tell menu bar item -2 to tell menu 1 # Window - Fenêtre
			click menu item theWindow
		end tell # menu bar item -2 to tell menu 1
	end tell # menu bar
	tell window 1 # now, it's theWindow
		tell toolbar 1
			click (first button whose description = sendButton)
		end tell
	end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 30 mars 2019 22:22:53

I’m not following what you mean. It currently appears that you’ve elected to discard a template that scripts the Mail.app to generate and send an outgoing message, in favour of a UI scripting technique. You haven’t actually identified any problem you’re having with the above template.

If the script which creates the message doesn’t issue the send command by itself, every attempts to send it by script later without using GUI scripting failed.
Thanks to insomnia I got it:

tell application "Mail"
	try
		set theMessage to (get first outgoing message)
		tell theMessage
			send
		end tell
	on error errMsg number errNbr
		display dialog errMsg & linefeed & "error # " & errNbr
	end try
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 31 mars 2019 05:18:17

Guys,
Many thanks for your help.
I am trying to figure out a best solution for me

Yes, I suppose that would be true, if one is considering two separate scripts—one that creates the outgoing message, and one that sends it. You may be correct in assuming this to be the situation for the OP, although there hasn’t been any indication that this is the case.

By the way, your above script can be simplified to this:

tell application "Mail" to tell the first outgoing message to if it exists then send

I’m trying to consider how such a situation would arise where this would be necessary, but I’m at a loss: why would the script that creates an outgoing message not execute the send command; and if it doesn’t, what necessitates needing to compose a second script just to do that ?

Reiterating my earlier recommendation, I would advise staying clear of pretty much any script that relies on scripting the UI, especially if there are other methods, but also if there aren’t. If you create the outgoing message by way of a script, then add the send commend to that; if you create the outgoing message manually, Yvan’s demonstrated how a script might be able to reference it by: set theMessage to (get first outgoing message). But if the only purpose of a script is to send the message, I’m not seeing the benefit automation brings over clicking “Send” yourself, which will likely be fewer clicks than are required to trigger a script to do this.

Of course, if you provide a little more background about what else is happening in your script, and what leads you to a situation where you have an unsent outgoing message, and what occurs between that point and the point you realise sending the message is appropriate now where it wasn’t before, that’ll be helpful in knowing what solution is more suitable, and whether we’ve overlooked another potential solution.

One reason for separating the script into two ones is the need to use a sender which is not the default one.
Here I have 4 mail addresses.
main@mac.com
aux1@orange.fr
aux2@laposte.net
aux3@machin.fr

If I try to use one of the 2nd or the 3rd auxiliary sender in the script creating the message, Mail always use the main address.
It’s boring but I must live with that.
So, I wasn’t surprised by the original question asking explicitly for a way to click the Send button.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 1 avril 2019 11:45:29

I faced a case when four messages waited to be sent but only two were identified as outgoing ones.
So I designed an alternate scheme to reach the one to send.

(*

https://macscripter.net/viewtopic.php?id=46794

*)

my Germaine()

on Germaine()
	set myDelim to space & character id 128236 & space
	
	tell application id "com.apple.Mail"
		# Get the localized name of the send button
		set SendLoc to localized string "ToolbarSend" --> "Envoyer" 
		set ToLoc to localized string "ts0-pD-Elh.title" from table "ComposeViewController" --> "À :"
		
		set outgoingMessages to get every outgoing message --> {}
		set theWindows to name of windows
		--> {"new subject4", "new subject", "Jusqu'à -75 pourcent plus un cadeau — désabonnés", "new subject2", "new subject", "Archives (2 messages)", "Brouillons (4 brouillons)"}
		if theWindows = {} then error number -128 # Cancel
	end tell # "Mail"
	
	tell application "System Events" to tell process "Mail"
		tell menu bar 1 to tell menu bar item -2 to tell menu 1 # Window - Fenêtre
			set menuItems to name of every menu item
			--> {…, "Tout ramener au premier plan", "Mettre au premier plan", missing value, "Archives (2 messages)", "Brouillons (4 brouillons)", "Jusqu'à -75 pourcent plus un cadeau — désabonnés", "new subject", "new subject2", "new subject", "new subject4"}
			# As you may see, the windows aren't ordered the same way than above.
		end tell # menu bar item -2 to tell menu 1 to tell menu bar
		--end tell
		
		set theWindows to {}
		repeat with anItem in reverse of menuItems
			set anItem to contents of anItem
			if anItem is missing value then exit repeat
			copy anItem to end of theWindows
		end repeat
		
		set theOutgoingMsgs to {}
		--tell application "System Events" to tell process "Mail"
		set frontmost to true
		repeat with i from 1 to count theWindows
			set aWindow to theWindows's item i
			tell window aWindow
				tell toolbar 1 to set maybe to exists (first button whose description = SendLoc)
				if maybe then
					-- get class of every UI element--> {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 name of every text field --> {"À :", "Cc :", "Cci :", "Répondre à :", "Objet :"}
					-- get (properties of (first text field whose name is ToLoc))
					-- get class of UI elements of (first text field whose name is ToLoc) --> {text field}
					set target to value of text field 1 of (first text field whose name is ToLoc)
					# create a list of object & target & index. This way we will be able to choose the correct message
					copy (aWindow & myDelim & target & myDelim & -i) to end of theOutgoingMsgs
				end if
			end tell # window
		end repeat
	end tell # System Events…
	
	tell application id "com.apple.Mail"
		--> {"new subject4 ???? dest AA ???? 1", "new subject ???? Dest BB ???? 2", "new subject2 ???? Dest CC ???? 3", "new subject ???? Dest BB ???? 4"}
		set theWindow to choose from list theOutgoingMsgs with title "Select the subject of the message to send"
		if theWindow is false then
			error number -128 # Cancel
		else
			set theWindow to theWindow's item 1 --> "new subject -3"
		end if
	end tell # Mail
	
	set {theObject, theTarget, theIndex} to my decoupe(theWindow, myDelim)
	set theIndex to theIndex as integer
	
	tell application "System Events" to tell process "Mail"
		set frontmost to true
		tell menu bar 1 to tell menu bar item -2 to tell menu 1 # Window - Fenêtre
			click menu item theIndex # brings the message at front
		end tell # menu bar item -2 to tell menu 1 to tell menu bar 1
		--get name of window 1# now, it's theObject
		tell window 1 to tell toolbar 1
			--get description of every button --> {"Envoyer", "Répondre", "Joindre", "Inclure les pièces jointes", "Format", "Navigateur de photos", "Afficher les modèles"}
			click (first button whose description = SendLoc)
		end tell # window
	end tell # System Events…
end Germaine

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 1 avril 2019 15:28:56

I corrected an error in the calculation of index of the menu item to click.

As I was puzzled by Mail behavior, I added one instruction in the script posted above.

It’s really foolish. The added instruction

set outgoingMessages to get every outgoing message --> {}

return an empty list but the existing windows are :
→ {“new subject4”, “new subject”, “Jusqu’à -75 pourcent plus un cadeau — désabonnés”, “new subject2”, “new subject”, “Archives (2 messages)”, “Brouillons (4 brouillons)”}

and four of them :
→ {“new subject4”, “new subject”, “new subject2”, “new subject”}
have a send button.

What may explain that ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 2 avril 2019 11:55:42

The slow, rotting carcass of AppleScript left for dead due to neglect…? I’m honestly not surprised, and these occurrences will only grow in number and frequency from now on.

Though I will never bring myself to write a GUI script. There are many languages out there, and I would recommend anyone who appreciates AppleScript to investigate Lua and Hammerspoon (Hammerspoon being the automation engine scripted using Lua). I’ll certainly miss AppleScript, but it won’t be hard to replace.

After-thought: Are any of those windows from drafts that were loaded manually, and are the new message windows generated through script or in the application ? I can’t recall for sure as I haven’t really used Apple Mail in a long time, but the AppleScript outgoing messages object might only contain messages generated by AppleScript, and within the instance of the script currently running. I could equally just have made that up, though, so I’m not claiming this as fact.

The four messages were created when I tested the behavior of the sender property defined by an applescript.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 3 avril 2019 12:26:09

You enumerated outgoing messages in that same script ? If so, and it returned an empty list, that’s Mail decomposing. Even if you enumerated the object in a subsequent script (without quitting Mail in between), that’s still appalling and disheartening, and beyond our control.

I didn’t quitted Mail between the creation step and the use of the script trying to get the messages to send.

It’s why I decided to use GUI Scripting which appeared to be the unique consistent scheme.
I’m 75 years old so I have no plan to learn a new language. I have already some difficulties to remember how to use those which I know for years.

When I use GUI scripting I try to do my best to build a stable code.

It’s why here I use menu -2 for the menu Window which seems to be an index which will not change. Not sure for index 9.

For the Send button, I extracts its localized name for the application’s resources.
Same thing for the “To :” name of field.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 3 avril 2019 15:20:13