Hi everyone, thank you for taking a moment of your time.
Newbie here, working on a solution for 2 weeks and have googled so much, I think I found the end of the internet.
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. else donât .
set astid to AppleScript's text item delimiters
set x to 2
set startHere to "p class=\"webNumber\">"
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
So I have been working diligently to do this by using the forums here, looking at other code- but I have hit a brick wall (and now I have a dent in my forehead) :(
Here is an excerpt of the HTML so you can see. this repeats on and on.
I would be so grateful to anyone that could help me get this going. 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.
Hi Yvan, thanks for your reply. I had modified the sample HTML for security reasons, and I updated it in the applescript. My challenge is understanding how to make the script repeat thru the rest of the HTML and get all the order numbers - BUT I havenât figured out how I would use IF-statment do to so.
Save this first script as a Script bundle in â~/Library/Script Librariesâ
Name the file substring.
Then click the âBundle contentsâ button that is now enabled at the top of the title bar.
( there is a post here that shows an image of this here)
Check the Applescript/Objective - C Library check box
re save the file. and Close it.
on expandTilde(thepath)
set theExpanded to (current application's NSString's stringWithString:thepath)'s stringByExpandingTildeInPath
return theExpanded as text
end expandTilde
on substrings(StartString, endString, theText)
try
set thePatten to current application's NSString's stringWithString:(StartString & "(.*?)" & endString)
set input to current application's NSString's stringWithString:theText
set err to missing value
set theCase to (current application's NSRegularExpressionCaseInsensitive)
set regEx to current application's NSRegularExpression's regularExpressionWithPattern:thePatten options:theCase |error|:err
set myArray to (regEx's matchesInString:input options:0 range:{location:0, |length|:length of theText})
set matches to current application's NSMutableArray's arrayWithCapacity:(count of myArray)
repeat with i from 1 to number of items in myArray
set match to item i of myArray
set matchRange to (match's rangeAtIndex:1)
(matches's addObject:(input's substringWithRange:matchRange))
end repeat
return matches as list
on error err
if err contains "Unrecognized function matchesInString_options_range_." then
set StartString to "\\\\" & StartString
set endString to "\\\\" & endString
error " Try escaping patten strings." & space & StartString & "..." & endString
end if
return err as text
end try
end substrings
Now open a new Applescript document and paste this second script code into it and run it.
tell script "substring" to set the path_ to expandTilde("~/Desktop/StorePurchases.webarchive")
set html to do shell script "cat " & quoted form of path_
set theOrderDits to {}
set theOrderNames to {}
tell script "substring"
set theOrderNames to substrings("From:", "]", html)
set theOrderDits to substrings("webNumber\">", "<", html)
end tell
do shell script "echo > ~/Desktop/returns.txt"
repeat with i from 1 to number of items in theOrderNames
set this_order to item i of theOrderNames & " = " & item i of theOrderDits
do shell script "echo " & quoted form of this_order & " >> ~/Desktop/returns.txt"
end repeat
This should produce a text file on the desktop.
I am working on the limited information you provided of the html. So come back to me if we need to work on this
Mark, First, Thank you so much for the time you devoted to helping. Iâve never used a bundle before. Please forgive me if I ask something novice. You have permission to e-slap me. haha
The first script gave an error Expected â,â but found â:â. in the following area,
set theExpanded to (current application's NSString's stringWithString:thepath)'s stringByExpandingTildeInPath
There was also no Objective C option, as illustrated in that related link.
I went ahead to the next script to look it over, should it begin with stell or tell . I know there are many commands Iâve never heard of, so I didnât know if it was typo or not.
Tested removing the S myself, and it complied. I did saved the bundle to the script libraries folder that I created, although the bundle options were not there- SO I tried on my other Mac running Mavericks and it looked like yours.
When I ran it, with much excitement and anticipation, It resulted with the error, Cant Make item 1 of {} into type Unicode text https://www.dropbox.com/s/i7dh7j7jf73ln7f/Screenshot%202014-02-02%2011.28.45.png
I am so far fascinated with the script and itâs ânew-nessâ and I am very stoked to be this close to a solution.
So I changed the script to match the source text, and it works GREAT, with one small exception- IT is pulling order numbers that match any order containing FROM, however, it doesnât get them all.
This is so far, the coolest and fastest script I have had the privilege of using.
That should be easy to fix. i am just looking at that.
i.e the first search will be set to find al text between webNumber and ]
Then it searches each return for the From: if it finds that it looks for the details in the text. saves it to file and moves on.
But running into a problem with I think line returns.
Still trying to figure that one out. If by chance you could show a couple entries as the really are in the .webarchive ( numbers and names changed etc.) that may help. You can put it up on your dropbox as a zip.
Thank you so much for helping with this script. And by helping, I mean, creating an amazing thing of beauty!
Creating a Script Library, as substring.bundle (something Iâve never seen before) But then again, Iâm a rookie in training
on expandTilde(thepath)
set theExpanded to (current application's NSString's stringWithString:thepath)'s stringByExpandingTildeInPath
return theExpanded as text
end expandTilde
on substrings(StartString, endString, theText)
try
set thePatten to current application's NSString's stringWithString:(StartString & "(.*?)" & endString)
set input to current application's NSString's stringWithString:theText
set err to missing value
set regexOptions to (current application's NSRegularExpressionUseUnixLineSeparators)
set regEx to current application's NSRegularExpression's regularExpressionWithPattern:thePatten options:regexOptions |error|:err
set myArray to (regEx's matchesInString:input options:0 range:{location:0, |length|:length of theText})
set matches to current application's NSMutableArray's arrayWithCapacity:(count of myArray)
repeat with i from 1 to number of items in myArray
set match to item i of myArray
set matchRange to (match's rangeAtIndex:1)
(matches's addObject:(input's substringWithRange:matchRange))
end repeat
return matches as list
on error err
if err contains "Unrecognized function matchesInString_options_range_." then
set StartString to "\\\\" & StartString
set endString to "\\\\" & endString
error " Try escaping patten strings." & space & StartString & "..." & endString
end if
return err as text
end try
end substrings
Then using this script to run-
tell script "substring" to set the path_ to expandTilde("~/Desktop/p1.webarchive")
set html to do shell script "cat " & quoted form of path_ as string
set theOrderDits to {}
set theOrderNames to {}
do shell script "echo > ~/Desktop/returns.txt"
tell script "substring"
set theOrder to substrings("webOrderNumber", "]", quoted form of html)
repeat with i from 1 to number of items in theOrder
set this_substrings to item i of theOrder
--log i
set theOrderNames to substrings("From:", "]", (this_substrings & "]"))
if theOrderNames is not {} then
log theOrderNames
set theOrderDits to substrings("webOrderNumber\">", "</p>", ("webOrderNumber" & this_substrings))
--log this_substrings
--log theOrderDits
-- commented out to try and show only Web Order Numbers
--set this_order to theOrderNames & " = " & theOrderDits as string
set this_order to theOrderDits as string
do shell script "echo " & quoted form of this_order & " >> ~/Desktop/returns.txt"
end if
end repeat
end tell
With the given sample file (saved on my desktop as mking.html) this script behaves flawlessly :
set astid to AppleScript's text item delimiters
set x to 2
set startHere to "p class=\"webNumber\">"
set stopHere to "</p>"
--set entireText to (do shell script "curl file:///Users/Michael/Desktop/StorePurchases.webarchive")
set entiretext to read file ((path to desktop as text) & "mking.html")
set writeFile to ((path to desktop as text) & "test_output") --
set AppleScript's text item delimiters to startHere
set blurb1 to text items of entiretext
repeat with i from 2 to (count blurb1)
set thisItem to item i of blurb1
set AppleScript's text item delimiters to stopHere
set blurb2 to text item 1 of thisItem
set AppleScript's text item delimiters to "[From: "
set blurb3 to text item 2 of thisItem
set AppleScript's text item delimiters to "]"
set blurb3 to text item 1 of blurb3
set success to appendDataToFile(blurb3 & " = " & blurb2, writeFile)
end repeat
set AppleScript's text item delimiters to astid
on appendDataToFile(myData, aFile)
set OA to (open for access aFile with write permission)
try
write (myData & linefeed) to OA starting at eof
close access OA
return true
on error
try
close access OA
end try
return false
end try
end appendDataToFile
Hi Yvan,
Thank you for looking at the script for me. I changed the one word back, that I provided, WebNumber, to the real value and the script did return two orders containing the word From. However it didnât return the others. Iâm not sure why at this time. So far the script from Mark has also returned some order numbers for me and I cannot thank you both enough for helping a newbie. In fact, I created a new script today, for something else, and I gained knowledge and insight based on the feedback you have given me. So thank you, I really appreciate your time and that you shared it with me.