Monday, May 20, 2019
  • Index
  •  » Automator
  •  » Write first name of email recipient in my mail reply

#1 2019-04-11 08:26:26 am

FayeVox
Member
Registered: 2019-02-11
Posts: 3

Write first name of email recipient in my mail reply

Hi All,

I have the below AppleScript. It works great but instead of producing "Hi Name" I would like it to produce "Hi Name,

"

IE: "Hi Brad,

"

Not "Hi Brad"

Everything I have tried doesn't work.

Hope you can help.

big_smile

Applescript:

tell application "System Events"
       tell process "Mail"
           keystroke "Hi "
           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

Offline

 

#2 2019-04-16 06:30:34 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 78

Re: Write first name of email recipient in my mail reply

I very rarely attend to UI scripts, so I'm not sure what called me to this one.  I drafted a script below that is for plain text emails, or emails that don't yet have any body content.  The value of the static text elements of the email body could not be edited, which would have been my first choice method; so I decided on a coin toss to do the edits by way of the clipboard, so that if the email did already have existing content, it would eliminate concern over accidentally inserting the greeting midway through the body.  However, the drawback is the data gets coerced to plain text, so any formatting will be lost.

Applescript:

use application "System Events"

property process : a reference to process "Mail"
property window : a reference to front window of my process
property scroll area : a reference to scroll area 1 of my window
property group : a reference to group 1 of UI element 1 of my scroll area

property _To : a reference to text fields of text field "To:" of my window
property _Hi : a reference to static text 1 of my group

property menu bar : a reference to menu bar 1 of my process
property menu : a reference to menu 1 of menu bar item "Edit" of my menu bar

property text item delimiters : ", "

-- If there are no recipients, there's no
-- value in inserting a greeting; therefore
-- terminate script
if not (_To exists) then return false
set recipients to the value of _To

-- Extract first name (word) of each recipient,
-- if it exists; replace with null if not
repeat with him in the recipients
       tell him to set [its contents] to (its words & null)
end repeat

-- Remove null values and insert a penultimate
-- "and" to serve as the conjunction between
-- the final two names
set recipients to text in recipients
tell (a reference to the recipients) to if its length > 1 then ¬
       set its contents to items 1 thru -2 & {["and ", item -1]}

-- Send focus to Mail and its front window
set frontmost of my process to true
perform my window's action "AXRaise"
# delay 0.2
set focused of my scroll area to true

-- Select, Cut, edit (on clipboard), and Paste
# delay 0.2
click menu item "Select All" of my menu
click menu item "Cut" of my menu
# delay 0.2
using terms from scripting additions
       if not (my group exists) then set the clipboard to linefeed
       set the clipboard to "Hi " & (recipients as text) & ¬
               [",", linefeed, linefeed] & (the clipboard)
end using terms from
click menu item "Paste" of my menu

The delay commands are commented out for you to see where I feel the potentially brittle points of the script lie.  Adjust them according to your needs during testing, to avoid having commands execute faster than the UI can accommodate, and resulting in either an unchanged, a blank, or an incorrectly pasted email body.

Also note, this script is tailored for an English language system.

If you would prefer the script to be modified to use keystrokes to insert the greeting (to preserve rich text formatting, or to avoid email content sitting on the clipboard), let me know and I can do that at some point.

AppleScript: 2.7
Operating System: macOS 10.13

Offline

 
  • Index
  •  » Automator
  •  » Write first name of email recipient in my mail reply

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)