------------------------------------------------------------------------------------------
# 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.
# 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 LISTS
------------------------------------------------------------------
------------------------------------------------------------------
# INDIVIDUALS
------------------------------------------------------------------
------------------------------------------------------------------
"
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
-------------------------------------------------------------------------------------------