Monday, January 22, 2018
  • Index
  •  » Code Exchange
  •  » Apple Mail » Compose Salutation & Paste into Front Outgoing Message

#1 2014-09-21 03:30:47 am

ccstone
Member
Registered: 2009-02-07
Posts: 409

Apple Mail » Compose Salutation & Paste into Front Outgoing Message

Hey Folks,

I've had some trouble with my Mail Salutation script since a recent system update, so I'm rewriting it.

This is far enough along for me to release into the wild, and hopefully someone else will find it as useful as I do.

I still cannot come up with a surefire means of addressing the front outgoing message using Mail's dictionary, so I finally broke down and used System Events.

The result is probably faster than the old script provided System Events is running and more accurate due to the certainty of getting data from the correct message.

Of course I'm using the Satimage.osax for regex support.  If you want to use something else please do, but you can write it yourself.  ;-)

I had to move the lookup-table out of the AppleScript code-block below to get it to format coherently.  Please put the table back into the correct spot indicated in the AppleScript code.

The regex that reads the table should recognize any horizontal whitespace as long as there are 2 or more between the email address and the associated salutation.

The labeled sections in the lookup-table used to serve a purpose, but they now are simply for convenience.

Note that table-entries may have more than one email address associated with the same custom salutation.

I've been using variations of this script for nearly 20 years and find it indispensable.  I don't understand why developers don't put more intelligence into their email clients, but then I cut my email teeth on Eudora.

--
Best Regards,
Chris

set lookupTable to "
--------------------------------------------------------------------------------------
# COMPANIES
--------------------------------------------------------------------------------------
alldritt@latenightsw.com, support@latenightsw.com             Hey Mark,
support@barebones.com                                                      Hey Guys,
--------------------------------------------------------------------------------------
# EMAIL LISTS
--------------------------------------------------------------------------------------
• applescript-users@lists.apple.com                                     Hey Folks,
• bbedit-talk@googlegroups.com                                          Hey Folks,
• keyboard_maestro@yahoogroups.com                                Hey Folks,
--------------------------------------------------------------------------------------
# INDIVIDUALS
--------------------------------------------------------------------------------------
listmeister@thestoneforge.com                                            Hey Chris,
--------------------------------------------------------------------------------------
"

Applescript:


------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2010/11/12 16:18
# dMod: 2014/09/21 03:03
# Appl: Apple Mail
# Task: Paste custom salutation using a lookup-table, munging the recipient's name,
# : or munging an email-list's attribution string.
# Osax: Satimage.osax [url]http://tinyurl.com/dc3soh[/url]
# Tags: @Applescript, @Mail, @Email, @Salutation
-------------------------------------------------------------------------------------------
# Notes:
-------------------------------------------------------------------------------------------
# The bullet character in the table defines the entry as an email-list.
# When the destination is an email-list the script looks for an attribution-string in the
# message body. If found it will use it to form the saluation.
# The regex for the attribution string may require adjustments for your system date set-up.
-------------------------------------------------------------------------------------------

try
   
   set lookupTable to <LOOKUP-TABLE> # Replace this line with the table above.
   set salutationStr to missing value
   
   tell application "System Events"
       if quit delay ≠ 0 then set quit delay to 0
       tell application process "Mail"
           tell front window
               if text field "To:" exists then
                   set msgRecipientList to value of text fields of text field "To:"
                   if msgRecipientList = {} then
                       if text field "BCC:" exists then
                           set msgRecipientList to value of text fields of text field "BCC:"
                       end if
                   end if
                   
                   # Unused ; Left as an example
                   # set msgSubject to value of text field "Subject:"
                   
                   tell groups of UI element 1 of scroll area 1
                       set AppleScript's text item delimiters to linefeed
                       set msgBodyText to (get value of its static text) as text
                       set AppleScript's text item delimiters to {""}
                   end tell
                   
               else
                   error "The front window is NOT an outgoing message!"
               end if
           end tell
       end tell
   end tell
   
   if length of msgRecipientList = 1 then
       set msgRecipient to item 1 of msgRecipientList
       set msgRecipientEmail to fndUsing("<([^>]+@[^>]+)>", "\\1", msgRecipient, 0, 1) of me
       
       set msgRecipientName to fndUsing("^([^<]+?) *<.+", "\\1", msgRecipient, 0, 1) of me
       
       set lookUpStr to fnd("^.*" & msgRecipientEmail & ".+", lookupTable, 0, 1) of me
       
       if lookUpStr ≠ false then
           set salutationStr to fndUsing("[[:blank:]]{2,}(\\S.+)$", "\\1", lookUpStr, 0, 1) of me
           
           if lookUpStr starts with "•" then
               set attributionString to fnd("On [a-z]{3} \\d{2}, \\d{4}.*wrote:.*", msgBodyText, false, true) of me
               if attributionString ≠ false then
                   set attributionString to cng("^.+, ", "", attributionString) of me
                   set _name to fnd("^\\w+", attributionString, 0, 1) of me
                   if _name ≠ false then
                       set salutationStr to "Hey " & _name & ","
                   end if
               end if
           end if
           
       else if lookUpStr = false then
           if msgRecipientName ≠ false then
               set msgRecipientName to cng(" .+", "", msgRecipientName) of me
               set salutationStr to "Hey " & msgRecipientName & ","
           end if
           
       end if
       
   else if length of msgRecipientList > 1 then
       # Stub for multiple to-recipients.
   end if
   
   # Paste salutationStr using System Events.
   if salutationStr ≠ missing value then
       set salutationStr to quoted form of (salutationStr & linefeed & linefeed)
       # Using `pbcopy` to prevent AppleScript from adding styled-text to clipboard.
       do shell script "pbcopy <<< " & salutationStr
       tell application "System Events"
           tell application process "Mail"
               set frontmost to true
               click menu item "Paste" of menu 1 of menu bar item "Edit" of menu bar 1
           end tell
       end tell
   end if
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
       try
           tell current application to button returned of ¬
               (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
                   default button "OK" giving up after 30)
           if ddButton = "Copy" then set the clipboard to e
       end try
   end if
end try

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on cng(_find, _replace, _data)
   change _find into _replace in _data with regexp without case sensitive
end cng
-------------------------------------------------------------------------------------------
on fnd(_find, _data, _all, strRslt)
   try
       find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
   on error
       return false
   end try
end fnd
-------------------------------------------------------------------------------------------
on fndUsing(_find, _capture, _data, _all, strRslt)
   try
       set findResult to find text _find in _data using _capture all occurrences _all ¬
           string result strRslt with regexp without case sensitive
   on error
       false
   end try
end fndUsing
-------------------------------------------------------------------------------------------

Offline

 
  • Index
  •  » Code Exchange
  •  » Apple Mail » Compose Salutation & Paste into Front Outgoing Message

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)