I use the below AppleScript in a Keyboard Maestro macro to insert the mail recipients name at the top of the email. But it only works when name is in format 'first name last name' e.g. 'Joe Bloggs'.
Frequently names of email recipients are in the format 'last name, first name' e.g. 'Bloggs, Joe'. How can I edit code so that if names are in format 'last name, first name' then the first name is inserted?
tellapplication "System Events"
tellprocess "Mail"
telltext field "To:" ofwindow 1
ifUI element 1 existsthen
set theToRecipient to (value ofUI element 1)
if (countwordsof theToRecipient) isgreater than 0 thenreturnword 1 of theToRecipient
Between the set theToRecipient... and if (count of words... add:
if theToRecipient contains "," then
set AppleScript's text item delimiters to ", "
set theToRecipient to text item 2 of theToRecipient
end if
Note that this way of doing things isn't 100% -- word 1 of "Mary-Anne Smith" will be "Mary" and not the correct "Mary-Anne", for example. If you are getting your address from another source (rather than this being a "Reply" email) see if you get the correct data from there at the same time.
Because the sender's "Reply To:" isn't standardised -- they may or may not include their real name, and if they do it could be in any order -- you're going to have to come up with your own set of rules. Have a look through some emails, but something like the following could work (pseudocode):
if theText contains a space -- there's a "real name" in there
if theText contains a comma
set text item delimiters to ", "
get the second text item
else
get the first text item
end if
else -- no "real name"
get the first "word" of the email address -- and pray!
end if
capitalise the first character of whatever we get
The first bit was done earlier. The tricky bit is getting the first "word" of an email address -- AS treats joe.bloggs as one word since there's no space after the .. So we go the other way and have a list of "allowed characters" in a word -- if we find a character that isn't in the list then we have a word break.
So:
set validChars to "0123456789abcdefghijklmnopqrstuvwxyz"
if (count words of theToRecipient) is greater than 0 then
if theToRecipient contains " " then
if theToRecipient contains ", " then
set AppleScript's text item delimiters to {", "}
set firstName to first word of text item 2 of theToRecipient
else
set firstName to first word of theToRecipient
end if
else
set x to 2
repeat while character x of theToRecipient is in validChars
set x to x + 1
end repeat
set firstName to characters 2 thru (x - 1) of theToRecipient as text
end if
if (ASCII number of character 1 of firstName) is greater than 96 then
set firstNameChars to every character of firstName
set item 1 of firstNameChars to (ASCII character ((ASCII number of item 1 of firstNameChars) - 32))
set AppleScript's text item delimiters to ""
set firstName to firstNameChars as text
end if
end if
return firstName
That copes with most addresses I've thrown at it -- see how you get on.
That doesn't seem to work for me if email address is something like joe.bloggs@gmail.com.
However, I have probably made an error in incorporating your code. Below is what code currently looks like. Where am I going wrong here?
tell application "System Events"
tell process "Mail"
tell text field "To:" of window 1
if UI element 1 exists then
set theToRecipient to (value of UI element 1)
if theToRecipient contains "," then
set AppleScript's text item delimiters to ", "
set theToRecipient to text item 2 of theToRecipient
end if
if (count words of theToRecipient) is greater than 0 then return word 1 of theToRecipient
end if
set validChars to "0123456789abcdefghijklmnopqrstuvwxyz"
if (count words of theToRecipient) is greater than 0 then
if theToRecipient contains " " then
if theToRecipient contains ", " then
set AppleScript's text item delimiters to {", "}
set firstName to first word of text item 2 of theToRecipient
else
set firstName to first word of theToRecipient
end if
else
set x to 2
repeat while character x of theToRecipient is in validChars
set x to x + 1
end repeat
set firstName to characters 2 thru (x - 1) of theToRecipient as text
end if
if (ASCII number of character 1 of firstName) is greater than 96 then
set firstNameChars to every character of firstName
set item 1 of firstNameChars to (ASCII character ((ASCII number of item 1 of firstNameChars) - 32))
set AppleScript's text item delimiters to ""
set firstName to firstNameChars as text
end if
end if
return firstName
end tell
end tell
end tell
More likely that I didn't have a decent handle on how the various address types were formatted after being grabbed from Mail. I've had another go, spoofing some addresses, so see if this works.
You'll find AS a bit easier to work with if you do things like putting "constants" at the top of your scripts and "closing" your tell blocks as soon as you don't need to reference that app/OSAX any more (if only because some apps have keywords that are similar to some "basic" AS ones, which can get confusing!).
Have a go with this, a full rewrite of what you've posted:
set validChars to "0123456789abcdefghijklmnopqrstuvwxyz"
tell application "System Events"
tell process "Mail"
tell text field "To:" of window 1
if UI element 1 exists then
set theToRecipient to (value of UI element 1)
end if
end tell
end tell
end tell
if (count words of theToRecipient) is greater than 0 then
if theToRecipient contains " " then
if theToRecipient contains ", " then
set AppleScript's text item delimiters to {", "}
set firstName to first word of text item 2 of theToRecipient
else
set firstName to first word of theToRecipient
end if
else
set x to 1
repeat while character x of theToRecipient is not in validChars
set x to x + 1
end repeat
set firstValidChar to x
repeat while character x of theToRecipient is in validChars
set x to x + 1
end repeat
set firstName to characters firstValidChar thru (x - 1) of theToRecipient as text
end if
if (ASCII number of character 1 of firstName) is greater than 96 then
set firstNameChars to every character of firstName
set item 1 of firstNameChars to (ASCII character ((ASCII number of item 1 of firstNameChars) - 32))
set AppleScript's text item delimiters to ""
set firstName to firstNameChars as text
end if
end if
return firstName