Automators 2: Email Automation

Thought you all might be interested in a tweak I made to this script. Often I’m emailing more than one person, especially when I’m replying to a group email thread. Depending on how many people I’m emailing, I start with one of these:

  • Hi (name),
  • Hi both,
  • Hi all,

Here’s the tweaked script that handles this:

tell application "System Events"
	tell process "Mail"
		tell text field "To:" of window 1
			get count of UI elements
			if result is 1 then
				set theToRecipient to (value of UI element 1)
				return word 1 of theToRecipient
			else if result is 2 then
				return "both"
			else if result is greater than 2 then
				return "all"
			end if
		end tell
	end tell
end tell

Then I insert the result into a snippet using Keyboard Maestro (because I don’t have TextExpander).

1 Like

Thanks for the suggestion @sylumer - but I’m afraid I don’t know what you mean as I have zero experience with AppleScript. Could you elaborate with an example?

Thanks!

Assuming that the script in question is the one here, then I woud be putting in display dialog lines something like this:

tell application "System Events"
	tell process "Mail"
		tell text field "To:" of window 1
			if UI element 1 exists then
				set theToRecipient to (value of UI element 1)
				display dialog "theToRecipient is currently set as: #" & theToRecipient & "#"
				display dialog "that is " & (count words of theToRecipient) & " words"
				if (count words of theToRecipient) is greater than 0 then
					display dialog word 1 of theToRecipient & " is the first word"
					if (count words of theToRecipient) is greater than 1 then
						display dialog word 2 of theToRecipient & " is the second word"
					end if
					return word 1 of theToRecipient
				end if
            end if
        end tell
    end tell
end tell

Unfortunately I don’t use Mail on my Mac so can’t test the script (also I’m actually just jotting this down on iOS - so it is entirely untested) and without the ‘raw data’ that you’re dealing with in terms of what the script is picking up it would be hard to determine.

But hopefully the script above should pop up a window telling you the content of the theToRecipient variable being picked up from the To field in mail which should be displayed between two hash/pound/sharp/octothorpe symbols … just to show if there’s actually any content at all. Then It should display the number of words making up that variable. If there’s at least one word it will then display that and on the off chance there is more than one it will also display the second … hopefully that will give you the key insight as to what is going on in relation to your recipients.

My stab in the dark would be that there is nothing being returned and you have zero words for the recipient. I think TextExpander would then be inserting nothing from this script and only the subsequent space followed by the comma. But the aim of the display dialogs is to give you that visual feedback.

Should you start writing lots of AppleScripts, then rather than using lots of display dialogs, using a script debugger is a quicker way to help fix issues before you start using a script within TextExpander.

Hope that clarifies enough for you to make some progress.

Thank you again @sylumer

I used your script and got the message script failed to compile written into the email body.

I then pasted your script into the Mac OS Script Editor, clicked compile and found it compiled without an issue. I then ran the script in the Script Editor and it returned the name of the person in the To field of the compose window correctly.

I then copy and pasted your script from the AppleScript editor back to TextExpander and found it then worked. I went through the same process for the original script and @Rockitude’s version. Both worked, but only after copying to Script Editor first, compiling, copying, then pasting back into TextExpander.

Thanks for the help!

1 Like

Thanks, that is a very interesting enhancement.

I was wondering if it is possible to develop so that, where there are only 2 addressees, both could be inserted instead of “both”. It just seems a bit impersonal to respond to two people as “both” rather than “Hi First Person and Second Person”.

I tried creating a "set the2Recipient to (value of UI element 2), but I am only guessing and don’t really know where to look. Anyway, the different iterations I tried either just returned the name of the first recipient or did not compile.

I implemented David’s applescript to get the name in Mail in Keyboard Maestro since I don’t have TextExpander, but I modified it slightly. Email addresses in my company are all of the form “Lastname, Firstname (Location) <firstname.lastname@company.com>” so I added a check for the name formatting. The script will return the first name for regular email addresses and company email addresses.

tell application "System Events"
	tell process "Mail"
		tell text field "To:" of window 1
			if UI element 1 exists then
				set theToRecipient to (value of UI element 1)
				if (count words of theToRecipient) is greater than 0 then
					if (offset of "," in theToRecipient) is greater than 0 then
						set theName to word 2 of theToRecipient
					else
						set theName to word 1 of theToRecipient
					end if
					set theGreeting to theName & ","
					return theGreeting
				end if
			end if
		end tell
	end tell
end tell
2 Likes

After several hours, I was able to get it to work for the latest version of Outlook for Mac using this code to replace David’s xnm script:

tell application "System Events"
	tell process "Outlook"
		tell text area 1 of scroll area 1 of splitter group 1 of window 1
			get count of buttons
			if result is 1 then
				set theToRecipient to (value of button 1)
				return word 1 of theToRecipient
			else if result is 2 then
				set the1stToRecipient to (value of button 1)
				set the2ndToRecipient to (value of button 2)
				return word 1 of the1stToRecipient & "/" & word 1 of the2ndToRecipient
			else if result is greater than 2 then
				return "all"
			end if
		end tell
	end tell
end tell

I also pulled in @Rockitude’s idea of seeing if there’s more than one person in the To: box. If there are two people it inserts FirstName1/FirstName2.

In the end, it works pretty great!

1 Like

If you correspond with people with hyphenated surnames, this variant on the original may be helpful.

I still can’t get this to work. I’m running Outlook 16.16.4. Since this is a work computer, I’m wondering if there’s a block of some sort that prevents it from working?

I modified Rockitude’s original script by borrowing from DJLein’s script for Outlook to get a more personal greeting where there are 2 addressees than “both”

tell application “System Events”

tell process “Mail”

tell text field “To:” of window 1

get count of UI elements

if result is 1 then

set theToRecipient to (value of UI element 1)

return word 1 of theToRecipient

else if result is 2 then

set the1stToRecipient to (value of UI element 1)

set the2ndToRecipient to (value of UI element 2)

return word 1 of the1stToRecipient & " and " & word 1 of the2ndToRecipient

else if result is greater than 2 then

return “all”

end if

end tell

end tell

end tell

Might be simple enough to personalise for 3 addressees as well.

Oh well, turns out it was not “simple enough” - at least for me.

When I modified the script to read as below, I don’t get first name greetings. Instead, the applescript itself is pasted into the mail message.

    tell application "System Events"
	tell process "Mail"
		tell text field "To:" of window 1
			get count of UI elements
			if result is 1 then
				set theToRecipient to (value of UI element 1)
				return word 1 of theToRecipient
			else if result is 2 then
				set the1stToRecipient to (value of UI element 1)
				set the2ndToRecipient to (value of UI element 2)
				return word 1 of the1stToRecipient & " and " & word 1 of the2ndToRecipient
			else if result is 3 then
				set the1stToRecipient to (value of UI element 1)
				set the2ndToRecipient to (value of UI element 2)
				set the3rdToRecipient to (value of UI element 3)
				return word 1 of the1stToRecipient & ", " & word 1 of the2ndToRecipient & " and " & word 1 of the3rdToRecipient
			else if result is greater than 3 then
				return "all"
			end if
		end tell
	end tell
end tell

If you are using TextExpander, make sure you have the snippet format set as AppleScript.

oh duh!

(Now it works.)

Thanks!

Or Typinator – works perfect!

Is there a possibility to use this script in fullscreen-mode? does not seem to work for me… :frowning:

I’ve had some odd issues with other applescript not working in full screen - snippets to get current browser URL etc. - I believe it’s because AS doesn’t recognise the full screen as window 1. Here’s how I fixed it in those:

# get the frontmost window - this will fail in full screen
try
tell application "Safari" to return name of current tab of window 1
return
end try

# get window 2 - for full screen
tell application "Safari" to return name of current tab of window 2

end run

Thanks for the answer! Strangely it worked with the script by iwarwick further up in this thread. That one works within full screen mode and window mode!

Hi Mark, Could you kindly share with me how you did that with KM? I’m very the much the noob and am waiting patiently for David’s new field guide to come out so that I can learn how to use this!

@RosemaryOrchard I would also love to see this macro, it would be a massive help for a project I am working on :blush:

Creating an e-mail, utilising Keyboard Maestro, based on a file appearing in a folder could be something as simple as this:

Use a folder trigger set to check for item additions. If it finds a new file, then it will simply utilise the send mail message action. I’ve used the %TriggerValue% variable as an example to show how you could use that to even include the file as an attachment to the mail.

I also set it to leave open so that I could show you the result of adding an image file to the watched folder.

If you wanted to check for a very specific file, you could do this by checking for the name for example with an if action.

Hope that helps.