Insert Airmail receiver's name

Hello,

I would like tyo reply my customers with automated answers using Insert text by pasting. In order to personalize the content, I would like to add the customer name into the text, for example:

Hello **Mark**, thanks for contacting us.

is it possible to get the sender's name and add it into my text?

Thank you

Short answer: Probably. :wink:

Unfortunately, I don't have the Airmail app (Airmail 3?), and there is very little scripting info about Airmail on the 'net. So, based on the scripting dictionary (SDEF) for Apple Mail, I have put together this script which you can test in Script Editor.

Select the message in your Airmail inbox that you want to reply to, and run this script.

tell application "Airmail 3"
  set msgList to selection
  set oMsg to item 1 of msgList
  
  tell oMsg
    --- sender property should be in this format ---
    --  "Sender Name <name@domain.tld>"
    set senderStr to sender
    
    --- Separate sender into Sender Name and Address ---
    set AppleScript's text item delimiters to {" <", ">"}
    set oSender to text items of senderStr
    set senderName to item 1 of oSender
    set senderAddr to item 2 of oSender
  end tell
end tell

return senderName

Post the results of your test back here, and we'll go from there. If you actually get the sender's name, the rest of what you want is fairly easy.

I did find one other KM forum topic about Airmail that could be helpful:
Insert subject line from message selected/opened in AirMail 3

Maybe @CafeTran_Training, or other Airmail user, can jump in here with a better answer.

BTW, if you'd like to take advantage of scripting, I highly recommend Script Debugger 7.
SD7 now comes with a 20-day free trial, and then will gracefully downgrade to free SD7 Lite (which is still much better than Script Editor).

Many thanks for your answer.

Unfortunately I got this error messager after selecting a message in Airmail 3 and running the script:

error "Airmail 3 got an error: Can’t get selection." number -1728 from selection

This could work:

   tell application "Airmail 3"

set theMessage to selected message

tell theMessage
	set theSubject to subject
	set the clipboard to theSubject
end tell
end tell

How to get the customer name instead of the subject title?

have you tried:

tell theMessage
  set theSubject to subject

  set senderStr to sender ### Customer

  set the clipboard to senderStr
end tell

If this does not work, please post a screenshot of the Airmail 3 Scripting Dictionary, with "message" selected. Should show all elements and properties of "message".

Hello, I tried it but didn't help. Actually it is not pasting my content, so I guess that the function is not correct. Sorry, I am not a developer and I am confused using Script Editor

I have uploaded a dictionary screenshot.

Thanks

Please post the exact script you used in the Script Editor, Put it in a forum Code Block.

I did not give you the full script above; only the changes. So, to be clear, here's the full script:


tell application "Airmail 3"
  set oMsg to selected message
  
  (*
  If the above fails, try this:
  set msgList to selection
  set oMsg to item 1 of msgList
  *)
  
  tell oMsg
    --- sender property should be in this format ---
    --  "Sender Name <name@domain.tld>"
    set senderStr to sender
    
    --- Separate sender into Sender Name and Address ---
    set AppleScript's text item delimiters to {" <", ">"}
    set oSender to text items of senderStr
    set senderName to item 1 of oSender
    set senderAddr to item 2 of oSender
  end tell
end tell

return senderName

Note that it does NOT paste anything. It simply returns the senderName to the Script Editor Event Log (which you may need to enable).

Run this in Script Editor.
If it fails, then please post:

  1. Exact error message
  2. Line that it failed on.

When you are viewing the AirMail scripting Dictionary, enter "message" into the search box, and then screen capture the results.

Hello, I tried all the options and these are the error messages I got:

	set oMsg to selected message

error "Can’t get item 2 of {"kmforum@forum.keyboardmaestro.com"}." number -1728 from item 2 of {"kmforum@forum.keyboardmaestro.com"}

set msgList to selection

error "Airmail 3 got an error: Can’t get selection." number -1728 from selection

set oMsg to item 1 of msgList

error "The variable msgList is not defined." number -2753 from "msgList"

I am not that fluent here with Keyboard Maestro, but I use Text Expander to respond to a customer with fill ins, on the Mac and in Windows, not sure if that would be any easier. I find this works great for responding to customer in regards to a quote, see below.

Re: RFQ %filltext:name=Machine Model% Jamestown Machinery
%key:tab%%key:tab%%key:tab%%key:tab%%key:tab%
Hi %filltext:name=person%,

Good %fillpopup:name=Salutation:Morning:Afternoon:Evening%, per your request on the %filltext:name=Machine Model%, %fillpopup:name=RFQ:default=please see attached proposal, pictures, and videos.:please see attached proposal and pictures.:this machine has recently been sold, sorry.:please see attached proposal.:this machine has recently been taken off the market, sorry.%

%fillpopup:name=RFQ Option_1:default=:However we do have a really nice,% %filltext:name=Optional Machine Model:default=% %fillpopup:name=RFQ Option_2:default=:could that work for you?%
%fillpopup:name=RFQ Option_3::Let us know, if that doesn’t work I can check with my colleagues here, to see what they are working on.%

Text_Expander

OK, I think I've done about all I can do without an actual copy of the AirMail 3 app.

But good news! I have been able to obtain a copy of the AirMail 3 Beta. So I will use that.

For anyone interested, download the beta here:
https://rink.hockeyapp.net/apps/84be85c3331ee1d222fd7f0b59e41b04

That's the official beta site provided to me by the Airmail support chat.

I'll get back to you soon.

Yes, but I hate the AirMail 3 scripting model. It makes very hard something that is very easy with every other email app I know.

So, AFTER you reply to the inbound email (your customer's email), run this script to get the sender's name:

tell application "System Events"
  tell application process "Airmail 3"  -- "Airmail Beta" in my case
    tell window 1
      tell scroll area 1
        tell text field 1
          set toRecipStr to (its value of item 1 of static text)
        end tell
        
      end tell
    end tell
  end tell
end tell

set AppleScript's text item delimiters to {" <", ">"}

set oToRecip to text items of toRecipStr
tell oToRecip
  set toNameStr to item 1
  set toAddressStr to item 2
end tell

return toNameStr

Now, let's put that in nice KM Macro.

Start by selecting the inbound email you want to reply to.
Please let us know if this works for you.


MACRO:   Reply to AirMail 3 EMail with Custom Message [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/2/6/262e0a5042fcfeae977c4c69bab663be9967da65.kmmacros">Reply to AirMail 3 EMail with Custom Message [Example].kmmacros</a> (6.2 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

Macro/Script Author:  @JMichaelTX

**HOW TO USE:**

   1. Select the Inbound EMail in AirMail that you wish to reply to.
   2. Trigger this macro

**SETUP:**

   * Confirm, or reset, the "Select Reply..." Menu to ensure it references your Airmail app.  
(I was using Airmail Beta).
   * Adjust the "Insert Text" as desired for pasting into the Airmail reply.

**REFERENCE:**

This macro built in response to this topic/post:

Topic Title:	Insert Airmail receiver's name
URL:		https://forum.keyboardmaestro.com/t/insert-airmail-receivers-name/10424?u=jmichaeltx

Date:		2018-05-28
Author:		retroriff

---

![image|529x1087](upload://8wZHdFyVQg1f7X38mvB6K8LSzZP.jpg)
2 Likes

It works like a charm! I'm totally impressed by the great help I received from you, and also by the powerful features KB has.

Just one little question to make this script even better. How could I just pick up the first name of the sender, instead of the full name? I would like to reply:

Hello Mick

instead of:

Hello Mick Jagger

Thank you very much!

JMichaelTX https://forum.keyboardmaestro.com/u/jmichaeltx
May 30

retroriff:

Hello, I tried it but didn't help. Actually it is not pasting my content,
so I guess that the function is not correct. Sorry, I am not a developer
and I am confused using Script Editor

Please post the exact script you used in the Script Editor, Put it in a
forum Code Block
https://forum.keyboardmaestro.com/t/how-to-paste-script-or-text-in-a-code-block/10091
.

I did not give you the full script above; only the changes. So, to be
clear, here's the full script:

tell application "Airmail 3"
set oMsg to selected message

(*
If the above fails, try this:
set msgList to selection
set oMsg to item 1 of msgList
*)

tell oMsg
--- sender property should be in this format ---
-- "Sender Name name@domain.tld"
set senderStr to sender

--- Separate sender into Sender Name and Address ---
set AppleScript's text item delimiters to {" <", ">"}
set oSender to text items of senderStr
set senderName to item 1 of oSender
set senderAddr to item 2 of oSender

end tell
end tell

return senderName

Note that it does NOT paste anything. It simply returns the senderName to
the Script Editor Event Log (which you may need to enable).

Run this in Script Editor.
If it fails, then please post:

  1. Exact error message
  2. Line that it failed on.

When you are viewing the AirMail scripting Dictionary, enter "message" into
the search box, and then screen capture the results.

1 Like

Replace:

return toNameStr

with

set posSpace to offset of " " in toNameStr
if (posSpace > 1) then
  set firstNameStr to text 1 thru (posSpace - 1) of toNameStr
else
  set firstNameStr to toNameStr
end if

return firstNameStr

Hello, I tried that but it didfn't work to me. Here's my macro:

Keyboard Maestro 8.2.1 “Reply to AirMail 3 EMail with Custom Message [Example]” Macro

Reply to AirMail 3 EMail with Custom Message [Example].kmmacros (6.5 KB)

Please be more specific. Exactly how "it didfn't work"?
Errors? Post exact error msg.
Unexpected results? Post expected and actual results.

Sorry, I checked my code and tried it again and now it works.

This macro will be very helpful.

Thank you.

1 Like

Some great and useful work here. I got some information over on automationorchard.com.

That enabled me to start a new email with a KM variable as a subject.

I'm now using this macro too (the modified one with the first name only). How would it be modified to take the full name of the sender and add that to a variable, and also to add the sender's email address to another variable? As I understand it, the modification to get the first name has meant that the full name isn't available anymore? (I really don't understand applescript!)

Thanks

OK, here's a more flexible macro/script that will return all of the sender's info, comma-delimited, into one KM Variable, which will work like an array:
FirstName,Full Name,Email address

Just replace the entire script in my above macro with this script:

Script to Get All Sender's Info of an AirMail Message

property ptyScriptName : "Get ToRecipient Name of AirMail Outbound Message"
property ptyScriptVer : "2.2" --  Return All Recipient Info as CSV List
property ptyScriptDate : "2018-06-13"
property ptyScriptAuthor : "JMichaelTX"

(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RETURNS:  |text| Name of Sender of Inbound Msg, now the To Recipient of the Reply Message.

PURPOSE/METHOD:
  • How To Use
      • Reply to an Inbound Message for which you want the Sender's Name
      • Then trigger this script.
  
  • Because the Airmail scripting model does not expose the sender's name property, or a reference to a new reply message, we must resort to UI scripting to get the sender's name, from a reply window.


REQUIRED:
  1.  macOS 10.11.6+
  2.  Mac Applications
      • Airmail 3   OR Airmail Beta

TAGS:  @Lang.AS @SW.KM @SW.Airmail @CAT.EMail @CAT.Messages @Auth.JMichaelTX

REF:  The following were used in some way in the writing of this script.
  1.  Airmail Beta app
      https://rink.hockeyapp.net/apps/84be85c3331ee1d222fd7f0b59e41b04
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
tell application "System Events"
  tell application process "Airmail 3" # "Airmail Beta" OR  "Airmail 3"
    tell window 1
      tell scroll area 1
        tell text field 1
          set toRecipStr to (its value of item 1 of static text)
        end tell
        
      end tell
    end tell
  end tell
end tell

set AppleScript's text item delimiters to {"  <", " <", "<", ">"}

set oToRecip to text items of toRecipStr
tell oToRecip
  set toNameStr to item 1
  set toAddressStr to item 2
end tell

set posSpace to offset of " " in toNameStr
if (posSpace > 0) then
  set firstNameStr to text 1 thru (posSpace - 1) of toNameStr
else
  set firstNameStr to toNameStr
end if

set scriptResults to {firstNameStr, toNameStr, toAddressStr}
set AppleScript's text item delimiters to ","
set scriptResults to scriptResults as text


return scriptResults

So now you could have:

image

TO: %Variable%Local__AirMailRecipient[2]%
EMail: %Variable%Local__AirMailRecipient[3]%

Hello %Variable%Local__AirMailRecipient[1]%, thanks for contacting us.

Note I have change the output of the Execute AppleScript
from: Local__ToRecipName
to: Local__AirMailRecipient

Questions?

1 Like

Thanks again @JMichaelTX. One more question. Sometimes the RecipName is not defined and this is an example of what the script returns:

Hello <gezdeojsgiyxymzzgy2tmml4gqydsnjwg42hy@reply.domain.com,

Is it possible to filter recipients who have not been defined, or exclude email addresses?

Thank you