Having trouble parsing a text file. Please help!

I am trying to write a script that will parse a paypal email. The problem is that only the first two sections work. Here is the script I have written:

set theFile to (choose file with prompt “Select a file to read:” of type {“TEXT”})
open for access theFile
set fileContents to (read theFile)
close access theFile
set oldDelimiters to AppleScript’s text item delimiters – always preserve original delimiters
set AppleScript’s text item delimiters to {“mailto:”}
set parseString to text items of fileContents
set parseString to item 2 of parseString
set AppleScript’s text item delimiters to {“)”}
set parseString to text items of parseString
set emailAddress to item 1 of parseString
display dialog emailAddress

set AppleScript’s text item delimiters to {“$”}
set parseString to text items of fileContents
set parseString to item 2 of parseString
set AppleScript’s text item delimiters to {" "}
set parseString to text items of parseString
set payment to item 1 of parseString
display dialog payment

set AppleScript’s text item delimiters to {“Item:”}
set parseString to text items of fileContents
display dialog item 1 of parseString
set parseString to item 2 of parseString
set AppleScript’s text item delimiters to {“!”}
set parseString to text items of parseString
set itemDescription to item 1 of parseString
display dialog itemDescription

set AppleScript’s text item delimiters to oldDelimiters – always restore original delimiters

The first two sections work but the third errors with “Can’t get item 2 of " …lists the entire text…”

The really weird thing is that if I copy and paste the text into another script editor window and try to run it, all three sections don’t work.

Any help would be appreciated.

Carey

Yow about posting the actual text along with the strings you want to extract?

I would like to, but every time I try to paste to this forum it only posts the first line.

Did you try putting the text in a

 wrapper?

Here is the text:

Dear Joe Smith,

This email confirms that you have received an eBay Instant Payment for
$4.99 USD from bogus (mailto:bogus@email.net)

View the details of this transaction online at:

https://www.paypal.com/us/vst/id=97M63188E4179010N


Item Information for bogus

Item Title: NEW! Italian Charm Key Tool - FREE Shipping!
Quantity: 1
Item URL: http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item=4918301085
Item #: 4918301085
Price: $4.99 USD


Subtotal $4.99 USD

Shipping & Handling: $0.00 USD
Insurance: –


Total Amount: $4.99 USD

Hi,

I don’t see “item:” in your text.

gl,

You are correct, there is no “Item:”. I must have pasted the code during my many attempts at troubleshoot. However, I changed it to “Item” and still have the same problem. At


display dialog item 1 of parseString

the dialog box displays the entire text instead of the portion before “Item”

Carey

Just another follow up. The third section works fine if I change the delimiter to any single character.

Carey

Hi,

It works here. I just changed “item:” to “item” in your original script and used the text as fileContents.

Quit Script Editor and start over with your original script. Wierd things can happen when you don’t trap your errors when changing tids.

gl,

set origText to "Dear Joe Smith, 

This email confirms that you have received an eBay Instant Payment for 
$4.99 USD from bogus (mailto:bogus@email.net) 

View the details of this transaction online at: 

https://www.paypal.com/us/vst/id=97M63188E4179010N 

------------------------------ 
Item Information for bogus 
------------------------------ 

Item Title: NEW! Italian Charm Key Tool - FREE Shipping! 
Quantity: 1 
Item URL: http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item=4918301085 
Item #: 4918301085 
Price: $4.99 USD 
_____________________ 
Subtotal $4.99 USD 

Shipping & Handling: $0.00 USD 
Insurance: -- 

_________________________ 
Total Amount: $4.99 USD
"

set emailAddress to do shell script "echo " & quoted form of the origText & " | tr -s '\r' '\n' | sed" & ¬
	" -e '/.*mailto:.*/!d' " & ¬
	" -e 's/^.*:\(.*@.*\....\).*$/\1/'"

set dollarAmount to do shell script "echo " & quoted form of the origText & " | tr -s '\r' '\n' | sed" & ¬
	" -e '/ from /!d'" & ¬
	" -e 's/^$\(.*\) USD.*$/\1/'"

set itemName to do shell script "echo " & quoted form of the origText & " | tr -s '\r' '\n' | sed" & ¬
	" -e '/Item Title: /!d'" & ¬
	" -e 's/^.*Title: \(.*\)!$/\1/'"