OS X Mail and Address Book

Hi.

If I have an email that says in the body:

Name - Patrick Shmatrick

Email - shmatrick@yahoo.com

Email repeat - shmatrick@yahoo.com

City - New Kensington

State - pa

Comments
I love your company.. can I work there some day?  This is a fake comment.

What is the best approach to capture first & last name, email, city, state, comments into variables that can then be pasted into a new card in address book? I already have a script working to create a card in address book, however, I am just not sure how to proceed to get all of the information off of a current email being viewed…

notice that there is a dash after each catagory except Comments— (I am basing this off of a web generated email that is sent when someone orders a product, so I don’t think I have much control on the Comments section…)

any help is greatly appreciated.

Hi Patrick,

Something like this would extract the relevant text from the text string:

set myText to "Name - Patrick Shmatrick

Email - shmatrick@yahoo.com

Email repeat - shmatrick@yahoo.com

City - New Kensington

State - pa

Comments
I love your company.. can I work there some day?  This is a fake comment.
"
repeat with y in paragraphs of myText
	if y begins with "Name - " then
		set first_name to word 2 of y
		set last_name to word -1 of y
	else if y begins with "Email - " then
		set email_addr to (characters ((offset of "-" in y) + 2) thru -1) of y as text
	else if y begins with "Email repeat - " then
		set email_addr_check to (characters ((offset of "-" in y) + 2) thru -1) of y as text
	else if y begins with "City - " then
		set the_city to (characters ((offset of "-" in y) + 2) thru -1) of y as text
	else if y begins with "State - " then
		set the_state to (characters ((offset of "-" in y) + 2) thru -1) of y as text
	end if
end repeat
set the_comments to (characters ((offset of "comments" in myText) + 9) thru -1) of myText as text

return {first_name, last_name, email_addr, email_addr_check, the_city, the_state, the_comments}

This does not account for people whose name contains more than two words, or compare the two email strings.

To get the contents of the first selected email from Mail.app this works (but only for text emails).

tell application "Mail"
	set theseMessages to selection
	set thisMessage to item 1 of theseMessages
	set myText to (content of thisMessage) as text
end tell

Best wishes

John M

Thanks for the reply and the help! Can you explain to me how the “offset” and -1’s all work?

-patrick

Hi Patrick,

I’ll try.

Offset is where the first occurence of a character (or sub-string) appears in a string. ie:

set y to "Email - shmatrick@yahoo.com"
offset of "-" in y -- returns 7

item -1 (ie character/word/paragraph) of a text string is the last one, item -2 would be the second to last, etc…

set y to "Name - Patrick Shmatrick"
word -2 of y -- returns "Patrick"

Breaking up the email extraction as far as possible:

set y to "Email - shmatrick@yahoo.com"
set myOffset to (offset of "-" in y)
-- returns 7
set myArray to (characters (myOffset + 2) thru -1) of y
-- returns {"s", "h", "m", "a", "t", "r", "i", "c", "k", "@", "y", "a", "h", "o", "o", ".", "c", "o", "m"}
set myText to myArray as text
-- returns "shmatrick@yahoo.com"

I hope this helps

John M

Thanks for the reply… Yeah that all makes sense except the -1… I still don’t quite get what that is referencing… (im talking about “characters (myOffset + 2) thru -1”)

… another question I have is…

What if the email form had:

email - patrick@shmatrick.com

date of birth - 7-18-76

How would I deal with it recognizing the “-” alone and not with numbers? Would the best way be do something like:

if y begins with "date of birth - " then set email_addr to (characters ((offset of " - " in y) + 2) thru -1) of y as text
??

-patrick

Hi Patrick,

-1 is the last character. Try running this in the script editor and looking at the result.

set x to "123456789"
set y to ((characters 5 thru -1) of x) as text

As for the date string paragraph, it would be the same. Offset finds the first occurence of a string, in this case “-”. Try running this:

set y to "date of birth - 7-18-76"
set birth_date to (characters ((offset of "-" in y) + 2) thru -1) of y as text
-- returns "7-18-76"

To obtain a valid date in Applescript from “7-18-76” you will need to change the date string around a bit.

John M

thank you so much for your time and help.

ok… Now I just realized that there is an addition email (from paypal) that I need to extract information from-- and from the looks of it, this is going to be much more difficult to extract data from… So any thoughts would be greatly appreciated…

Here is a paypal email-- I need to extract the email address, name, and address… How would you recommend doing this?

-patrick

Begin forwarded message:

From: "mitch@mitchwalker.net" <mitch@mitchwalker.net>
Date: June 24, 2005 3:54:03 AM PDT
To: Patrick Collins <patrickco@aol.com>
Subject: Item #001 - Notification of Payment Received from Mitch Walker (mitch@mitchwalker.net)

email_logo.gif (2KB) pixel.gif (1KB) pixel.gif (1KB)

Protect Your Account Info


Make sure you never provide your password to fraudulent websites.



To safely and securely access the PayPal website or your account, open a new web browser (e.g. Internet Explorer or Netscape) and type in the PayPal URL to be sure you are on the real PayPal website.https://www.paypal.com/us/) to be sure you are on the real PayPal site.



PayPal will never ask you to enter your password in an email.



For more information on protecting yourself from fraud, please review our Security Tips at https://www.paypal.com/us/securitytips

pixel.gif (1KB)

Protect Your Password


You should never give your PayPal password to anyone, including PayPal employees.

pixel.gif (1KB)

Dear Cameron Shayne Johnson,

This email confirms that you have received a credit card payment from Mitch Walker(mitch@mitchwalker.net).


View the details of this transaction online

pixel.gif (1KB)

Payment Details

pixel.gif (1KB)

Total Amount:

pixel.gif (1KB)

$18.44 USD
Currency:

pixel.gif (1KB)

U.S. Dollars
Transaction ID:

pixel.gif (1KB)

3F621195X6051354M
Sales Tax:

pixel.gif (1KB)

$0.00 USD
Total Shipping:

pixel.gif (1KB)

$3.45 USD
Item/Product Name:

pixel.gif (1KB)

Piano Beginning Practice DVD
Item/Product Number:

pixel.gif (1KB)

001
Buyer:

pixel.gif (1KB)

Mitch Walker

pixel.gif (1KB) pixel.gif (1KB) pixel.gif (1KB)

Shipping Information

pixel.gif (1KB)

Address:

pixel.gif (1KB)

Mitch Walker
34 Tonya Drive
Amherst, NY 14236
United States

pixel.gif (1KB)

Address Status:

pixel.gif (1KB)

Confirmed 

scr_symQuestion.gif (1KB)

pixel.gif (1KB) pixel.gif (1KB) pixel.gif (1KB) pixel.gif (1KB)

Thank you for using PayPal!
The PayPal Team


PayPal Email ID PP341

With Love and Respect

Patirck Collins
Founder & Creator
www.collinatorstudios.com

Patrick, if that is an actual email with a customer name and contact information, please edit it immediately to change the customer’s information to dummy data (i.e., John Doe, 123 Main Street, etc.).

the information is bogus, i already edited it.

-p