Automators 2: Email Automation

I can’t seem to get David’s XNM to work with Microsoft Outlook, which is based on the Episode 2 example.

The download from the show notes works perfect for Apple Mail, but I get (AppleScript did not compile) in Outlook. I searched online, but can’t find an answer. Any ideas?

This is what I initially tried to attempt:

tell application "Microsoft Outlook"
tell process "Microsoft Outlook"
		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 return word 1 of theToRecipient
			end if
		end tell
	end tell
end tell

Just a word of warning on 3rd party Email clients.

I’ve been hunting for a good replacement iOS email client for a long time. Airmail, Spark (both of which you mentioned), Newton and Edison all seemed to have interesting features. I actually used Spark on a test account for a trial and was very impressed.

However, after digging into the details I came across people very worried about their privacy policies and/or the actual architecture they use.

By necessity, if they offer push notifications then they require your credentials (or auth tokens) on their servers so they can determine if mail has arrived in your various accounts and also possibly during the act of collecting your mail. To be fair, those I looked at most closely are very open about this and explain how they mitigate the risk by encrypting these credentials while at rest.

However, since their services have the means to access your mail accounts independently, then there is always the possibility, however remote, that your passwords/tokens could leak out in a hack and your email security be breached. Since many services that have your banking and credit card details offer password reset by email then this is a potential problem. This also makes these companies a target for hackers.

Some of the services (I’d suggest investigating the free ones) also do analytics on your mail looking for shopping patterns and sell this data to fund the service.

Some also allow you to opt out of analytics and disable push which might be a solution, but that means you’re a couple of configuration settings away from the problem and you can’t see what’s really going on under the hood.

While I might prepared to take this risk (and I’m frequently tempted by the shiny features), this was an automatic show stopper for my job. I would literally be sharing my domain login password with a 3rd party without their consent, a sackable offence since our clients data is highly sensitive.

The only interesting 3rd party mail client I found that did not have this issue (as far as I know) is Dispatch, since it doesn’t support push - but sadly it also doesn’t support exchange, which I need.

Anyway I figured it’s best to share this since a lot of people who use these services are unaware of the issues, since they’re hardly lit in neon in the App Store.

This is broad summary of what I’ve read so If I’ve unfairly mischaracterised how any of these services work then let me know.

And of course if someone is using a secure iOS mail client with great features and exchange support then I’d like to hear about that too!

Thanks

2 Likes

I have my work account set up in Dispatch and that’s an Exchange server. From memory I set it up as an IMAP account (which we offer).

Thanks, It seems that for business Office365 accounts imap access is optional and it’s turned off on ours :frowning:

Another option is Canary. Of all the clients that support push notifications, this one had the least ambiguous policy about disabling push equating to not storing your credentials on their servers. But unfortunately it doesn’t support exchange.

I’ve downloaded David’s TextExpander Auto Name Email snippets. I’m excited to implement them into my workflow.

I’m using Mail and have not edited them, however rather than insert the name of the email recipient I only get a space before the comma - no name.

Has anyone solved a similar problem?

How many words are in the name of the recipient in the To field? The script is checking for the number of words in order to determine if there’s a name to be grabbed.

Does it fail for all (broadly) your reply e-mails or just ones to a handful of recipients?

There are two names. Although I have tried with one and got the same results.

I’ve tried different email accounts, domains, email address lengths and formats (eg emails with periods and without) and nothing seems to work.

I feel that I must be missing something obvious.

I haven’t seen a response to this request. I have the same interest. @RosemaryOrchard will this macro be made available? Thanks!

Try adding in some dialog display entries with various values temporarily so you can see show what the values are thatare being used by the script for this instance. That should help you figure it out. Wihtout seeing the exact data in the to field, it is hard to figure out what the AppleScript is actually processing.

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.