AppleScript parse HTML

Goal: Long list of purchase orders that I need to refund if they contain the word "From:" and each purchase has a order number and I have been trying to parse or loop, not sure of right word Search HTML doc for instance of the word "From:" and if found, then get other number and print to a text file.

So far, I was able to learn, sort of how to use text item delimiters, but when I tried making it loop or repeat, I just got the same order number over and over. This is before even trying to implement the part that says IF order details include word "from" then get order number.

set astid to AppleScript's text item delimiters
set x to 2

set startHere to "p class=\"webOrderNumber\">"
set stopHere to "</p>"

set entireText to (do shell script "curl     file:///Users/Michael/Desktop/StorePurchases.webarchive")
set AppleScript's text item delimiters to startHere

set blurb1 to text item x of entireText
repeat with i from 2 to (count of blurb1)
   set thisItem to item i of blurb1

   set AppleScript's text item delimiters to stopHere
   set blurb2 to text item 1 of blurb1

   set AppleScript's text item delimiters to astid



   set writeFile to ((path to desktop as text) & "test_output") -- 
   set writeData to blurb2

   set success to appendDataToFile(writeData, writeFile)
end repeat


    on appendDataToFile(myData, aFile)
       set OA to (open for access aFile with write permission)
   try
       write (myData & (ASCII character 10)) as text to OA starting at eof
       close access OA
       return true
   on error
       try
           close access OA
       end try
       return false
   end try
end appendDataToFile

Here is an excerpt of the HTML:

</td>
  <td class="OrderNumber">
<p class="webNumber">BHWL123456</p>
 </p>
    <td class="OrderNumber">

    <p class="details">
Full Version: Get all Activities &amp; Remove ads! [From: Pony Resort: MakeverMagic]     </p>
</td>

This is going to be used when there are hundreds of orders that would take too long to physically search for the word FROM and write the order number.

Answers


Try:

set resultNumbers to {}

set myText to read file ((path to temporary items as text) & "test.txt") as «class utf8»

set {TID, text item delimiters} to {text item delimiters, "<p class=\"webNumber\">"}
set webOrderNumbers to text items 2 thru -1 of myText

set AppleScript's text item delimiters to "</p>"
repeat with wNumber in webOrderNumbers
    set end of resultNumbers to text item 1 of wNumber
end repeat

set text item delimiters to TID
return resultNumbers

This question was asked and solved afaik on Macscripter.net with the use of a Applescript Script library and additional Applescript I provided in this thread.

The scripting library uses NSRegularExpresion to search between two text targets and return all occurrences in one go.

I can post the code here if anyone is interested or you can visit the thread.


Need Your Help

Get a exception of "error connecting with login URI" when running Google Data's example

java gdata google-data-api google-data

When I run the example CalendarFeedDemo in the JAVA package of the Google Data API client library, I've encounterd a exception like this

How to get the the current number of row using mouseover in JQgrid?

jqgrid row

I want to get the number of row by mouseover. This is my code :