Wednesday, July 24, 2019

#1 2014-02-02 11:53:37 am

mking0310
Member
Registered: 2013-11-12
Posts: 10

Req Help making script that uses TID and If-then

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. wink

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 .

Applescript:


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.

</td>
      <td class="OrderNumber">

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

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


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.

Thank you in advance, 
Michael

Last edited by mking0310 (2014-02-02 12:13:19 pm)

Offline

 

#2 2014-02-02 12:11:31 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3472

Re: Req Help making script that uses TID and If-then

I guess that it would be useful to edit the instruction :

set blurb1 to text item x of entireText

as

set blurb1 to text item x thru -1 of entireText

I just guess because the startHere string is unavailable in your sample file.
I assume that it's supposed to be available several times.

Yvan KOENIG (VALLAURIS, France) dimanche 2 février 2014 19:10:41

Offline

 

#3 2014-02-02 12:15:27 pm

mking0310
Member
Registered: 2013-11-12
Posts: 10

Re: Req Help making script that uses TID and If-then

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.

Gratefully,
Michael

Offline

 

#4 2014-02-02 01:01:46 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Req Help making script that uses TID and If-then

You could try this.


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.

Applescript:



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.



Applescript:


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

Last edited by mark hunte (2014-02-02 01:08:59 pm)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#5 2014-02-02 01:14:46 pm

mking0310
Member
Registered: 2013-11-12
Posts: 10

Re: Req Help making script that uses TID and If-then

smile  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,

Applescript:

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.

Thank you Mark !

Sincerely,
Michael

Offline

 

#6 2014-02-02 01:20:07 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Req Help making script that uses TID and If-then

I spotted the extra "s" and updated it . refresh and you should get it as it should be or remove the "s" yourself.


When you save the first script  the save dialog will show a file format:   option.

Set this to Script Bundle


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#7 2014-02-02 01:30:49 pm

mking0310
Member
Registered: 2013-11-12
Posts: 10

Re: Req Help making script that uses TID and If-then

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/i7dh7j7jf73ln … .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.

Offline

 

#8 2014-02-02 01:36:37 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Req Help making script that uses TID and If-then

Ok.  that means it is not finding your order number.




The html I was using is


</td>
      <td class="OrderNumber">

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

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

</td>
      <td class="OrderNumber">

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

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

</td>
      <td class="OrderNumber">

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

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


This give the end result  text file with:

Pony Resort: CCMagic = C1WL123456
Pony Resort: MakeverMagic = BHWL123456
Pony Resort: CanMagic = AHWL123456


I would need more detail of the real mark up to pin point the problem.

Last edited by mark hunte (2014-02-02 01:38:26 pm)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#9 2014-02-02 01:44:41 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Req Help making script that uses TID and If-then

p.s,


I wil also work on the if condition. As I realise you will need that to distinguish orders with or without the From:


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#10 2014-02-02 02:02:44 pm

mking0310
Member
Registered: 2013-11-12
Posts: 10

Re: Req Help making script that uses TID and If-then

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.

Offline

 

#11 2014-02-02 02:12:43 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Req Help making script that uses TID and If-then

Great.

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.


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#12 2014-02-02 02:21:23 pm

mking0310
Member
Registered: 2013-11-12
Posts: 10

Re: Req Help making script that uses TID and If-then

I sent you  the Dropbox URL via PM

Offline

 

#13 2014-02-03 08:21:41 am

mking0310
Member
Registered: 2013-11-12
Posts: 10

Re: Req Help making script that uses TID and If-then

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

Applescript:

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-

Applescript:



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





Offline

 

#14 2014-02-03 08:29:19 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3472

Re: Req Help making script that uses TID and If-then

With the given sample file (saved on my desktop as mking.html) this script behaves flawlessly :

Applescript:


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

Yvan KOENIG (VALLAURIS, France) lundi 3 février 2014 15:27:25

Offline

 

#15 2014-02-03 03:23:07 pm

mking0310
Member
Registered: 2013-11-12
Posts: 10

Re: Req Help making script that uses TID and If-then

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.

Sincerely,
Michael

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)