Thank you for sharing the references! They were extremely helpful. I have put together the code below to extract more information using the delimiters that were available in email sent in. Example below had double “Sincerely,” but have double space bars to delimit city name and street.
set thing to "Thank you for the opportunity to express my support
Sincerely,
Sincerely,
John Doe
123 Road Dr City Name, ST 12345-6789
jdoe@email.com"
set counter to 1
repeat until thing's paragraph counter begins with "Sincerely,"
set counter to counter + 1
end repeat
tell thing to set {nom, email, cityLine} to {paragraph (counter + 3), paragraph (counter + 5), paragraph (counter + 4)}
set setoff to offset of "," in cityLine
set city to cityLine's text 1 thru (setoff + 3)
set AppleScript's text item delimiters to ","
set city2 to text item 1 of city
set AppleScript's text item delimiters to return
set AppleScript's text item delimiters to " "
set city3 to text item 2 of city2
set AppleScript's text item delimiters to return
set state to cityLine's text (setoff + 2) thru (setoff + 3)
{nom, email, city3, state}
This works pretty well for the most part when by passed with the try argument, however when I try to embed this onto my other script it changes some parts of it mainly the text getting changed from text to rich text. Which causes an error “Can’t get text 1 thru 13 of…”
tell application "Mail"
set theRow to 2
get account
set theMessages to messages of mailbox "Inbox"
repeat with aMessage in theMessages
set thing to content of aMessage as rich text
set counter to 1
repeat until thing's paragraph counter begins with "Sincerely," --change this according to the ending salutation5
set counter to counter + 1
end repeat
tell thing to set {nom, email, cityLine} to {paragraph (counter + 1), paragraph (counter + 3), paragraph (counter + 2)}
set setoff to offset of "," in cityLine
set city to cityLine's rich text 1 thru (setoff + 3)
my SetName(nom, theRow, theSheet)
my SetCity(city, theRow, theSheet)
my SetEmail(email, theRow, theSheet)
my SetDate(date received of aMessage, theRow, theSheet)
my SetFrom(sender of aMessage, theRow, theSheet)
my SetSubject(subject of aMessage, theRow, theSheet)
my SetMessage(content of aMessage, theRow, theSheet)
my SetReply(reply to of aMessage, theRow, theSheet)
my SetRecipient(address of first recipient of aMessage, theRow, theSheet)
set theRow to theRow + 1
end repeat
end tell
I think this is because of how I am extracting the message from the inbox and my lack of understanding of the different classes? in scripting. See the script embedded between the try end try argument.
tell application "Mail"
set theRow to 2
get account
set theMessages to messages of mailbox "Inbox"
repeat with aMessage in theMessages
try
set thing to content of aMessage as rich text
set counter to 1
repeat until thing's paragraph counter begins with "Sincerely," --change this according to the ending salutation5
set counter to counter + 1
end repeat
tell thing to set {nom, email, cityLine} to {paragraph (counter + 3), paragraph (counter + 5), paragraph (counter + 4)}
set setoff to offset of "," in cityLine
set city to cityLine's rich text 1 thru (setoff + 3)
my SetName(nom, theRow, theSheet)
my SetCity(city, theRow, theSheet)
my SetEmail(email, theRow, theSheet)
end try
my SetDate(date received of aMessage, theRow, theSheet)
my SetFrom(sender of aMessage, theRow, theSheet)
my SetSubject(subject of aMessage, theRow, theSheet)
my SetMessage(content of aMessage, theRow, theSheet)
my SetReply(reply to of aMessage, theRow, theSheet)
my SetRecipient(address of first recipient of aMessage, theRow, theSheet)
set theRow to theRow + 1
end repeat
end tell
I’m running against a similar issue with the code below. What I do not understand is if I try to embed the argument below, my code doesn’t work. For some reason it doesn’t like the to argument being in there.
Edit: I just checked the dictionary and there is a to property, although it is part of the standard suite. Maybe I just don’t have a clue.
to extractBetween(SearchText, startText, endText)
set tid to AppleScript's text item delimiters -- save them for later.
set AppleScript's text item delimiters to startText -- find the first one.
set endItems to text of text item -1 of SearchText -- everything after the first.
set AppleScript's text item delimiters to endText -- find the end one.
set beginningToEnd to text of text item 1 of endItems -- get the first part.
set AppleScript's text item delimiters to tid -- back to original values.
return beginningToEnd -- pass back the piece.
end extractBetween