Greetings,
I receive email orders with field names and field values, which used to show up as:
Name-----------: John Q.Public
Email-----------: jqp@email.com
Phone----------: 123-456-7890
My script would basically ask to define a field, and get the field value:
if i starts with "Name--" then
set customerName to text 36 thru length of i
The emails are now set up differently (I have no control over this), and now they show up broken into 2 lines as:
Name--------
—: John Q.Public
Email-------
----: jqp@email.com
Phone--------
–: 123-456-7890
Does anyone know of a good way to script this now that the field value is on the next line after the field name?
Hi,
try this
set theText to "Name--------
---: John Q.Public
Email-------
----: jqp@email.com
Phone--------
--: 123-456-7890"
set theList to {}
repeat with i from 1 to (count paragraphs of theText) by 2
set firstItem to paragraph i of theText
set secondItem to paragraph (i + 1) of theText
set end of theList to {text 1 thru ((offset of "-" in firstItem) - 1) of firstItem, text ((offset of ":" in secondItem) + 2) thru -1 of secondItem}
end repeat
--> {{"Name", "John Q.Public"}, {"Email", "jqp@email.com"}, {"Phone", "123-456-7890"}}
Thanks Stefan.
I keep getting errors. I should tell you I select an email message in Mail.app, then run the script on the selected email.
Here are four errors that vary depending on which email I select:
Can’t get text 2 thru -1 of “”.
Can’t get text 1 thru -1 of “”.
Can’t get paragraph 50 of (email data shows up in script editor’s Applescript Error display)
Can’t get paragraph 46 of (email data shows up in script editor’s Applescript Error display)
Here is the part of my script to set text variables, with your contribution which I edited for the text variable:
tell application "Mail" to set sel to selection
repeat with oneMail in sel
tell application "Mail" to tell oneMail to set {theContent, theSubject, theSender} to {paragraphs of content, subject, extract name from sender}
end repeat
set theEmail to (theSender as text) & return & (theSubject as text) & return & (theContent as text)
set orderData to text of theEmail
set dateStamp to ((the current date) as string)
set theList to {}
repeat with i from 1 to (count paragraphs of orderData) by 2
set firstItem to paragraph i of orderData
set secondItem to paragraph (i + 1) of orderData
set end of theList to {text 1 thru ((offset of "-" in firstItem) - 1) of firstItem, text ((offset of ":" in secondItem) + 2) thru -1 of secondItem}
end repeat
my example assumes that the content of the mail is just the text you’ve posted.
This checks the paragraphs for beginning with the keywords Name, Email, Phone
.
set theList to {}
repeat with i from 1 to (count paragraphs of orderData)
set firstItem to paragraph i of orderData
if firstItem begins with "Name" or firstItem begins with "Email" or firstItem begins with "Phone" then
set secondItem to paragraph (i + 1) of orderData
set end of theList to {text 1 thru ((offset of "-" in firstItem) - 1) of firstItem, text ((offset of ":" in secondItem) + 2) thru -1 of secondItem}
set i to i + 2
else
set i to i + 1
end if
end repeat
Note: your script considers only the last mail in the selection, the data of the former one will be overwritten
Excellent!
Many thanks again Stefan.