• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Mail ; Compose and Paste Salutation String
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Mail ; Compose and Paste Salutation String


  • Subject: Mail ; Compose and Paste Salutation String
  • From: Christopher Stone <email@hidden>
  • Date: Sun, 21 Sep 2014 03:45:36 -0500

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 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 use tabs in my lookup-table here at home, but due to the way Mail formats things I've put in non-breaking spaces in the one below.  I don't know if they'll survive the trip across the Internet, but be prepared to change them to spaces or tabs.  The regex should recognize any horizontal whitespace as long as there are 2 or more between the email address and the associated 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

------------------------------------------------------------------------------------------
# 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 http://tinyurl.com/dc3soh
# Tags: @Applescript, @Mail, @Email, @Salutation
# Test: Only on OSX 10.9.5
-------------------------------------------------------------------------------------------
# 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 "
------------------------------------------------------------------
# COMPANIES
------------------------------------------------------------------
email@hidden, email@hidden       Hey Mark,
email@hidden                                   Hey Guys,
------------------------------------------------------------------
# EMAIL LISTS
------------------------------------------------------------------
• email@hidden                     Hey Folks,
• email@hidden                          Hey Folks,
• email@hidden                      Hey Folks,
------------------------------------------------------------------
# INDIVIDUALS
------------------------------------------------------------------
email@hidden                           Hey Chris,
------------------------------------------------------------------
"
   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
-------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: Convert a lot of files from eps to svg format without buying Illustrator
  • Next by Date: Re: A permission problem?
  • Previous by thread: Re: Can AppleScript determine if a shell process is running?
  • Next by thread: Search Contacts for phone number
  • Index(es):
    • Date
    • Thread