Search code examples
applescriptdelimitersubscript

run a sub-script for every item of a list on AppleScript


I have a list, and I want which every element of this list (order number) to run a search, grab text from chrome and navigate through a website using this order number.

I have already both script (A and B)

I thought I could just add my long script (B) to the first one, inside

repeat with theItem in theResult
Script B
end repeat

but this is not working, I get the error

Expected “end” but found “property"

Script B e.g :

tell application "Google Chrome"
    tell front window's active tab to set infoGrab to execute javascript "document.getElementsByClassName('even')[2].innerHTML;"
end tell

set theText to Unicode text
set theSource to infoGrab
property leftEdge72 : "<a href=\"/"
property rightEdge72 : "\">"
set saveTID to text item delimiters
set text item delimiters to leftEdge72
set classValue to text item 2 of theSource
set text item delimiters to rightEdge72
set uniqueIDKey to text item 1 of classValue
set text item delimiters to saveTID
uniqueIDKey

and more.

I then tried to save script B in an independent script and run from script A like this

repeat with theItem in theResult
    set the clipboard to theItem
    set myScript to load script file ((path to desktop folder as text) & "SEARCH.scpt")
    tell myScript
    end tell
    delay 30
end repeat

but this don't work neither, the script B since to ignore all repeat and delay and just run everything instantaneity with no action on goole chrome

Question : How to I get to do other action for each element of a list including text-delimiter and more.

PS : sorry if my post is confusing.


Solution

  • I think your script is flawed, since "Script B" does NOT use "theItem" variable from the repeat loop. So, "Script B" will return the same result for every item.

    Your objective is not clear. Perhaps if you provided more details, with real-world example data of both the source HTML and the expected results we could provide better help.

    IAC, why are you calling a script instead of using a handler? Here's your scripts refactored to use a handler, as an example.

    repeat with theItem in theResult
      ## You don't seem to use "theItem" in the Script B ##
      # if so, then the getID() handler will return the same results for all items in this loop
    
      set myID to my getID()
    end repeat
    
    on getID() -- was Script B
      -- put in same script file, or in script library
      -- does this need to have a parameter?
    
      tell application "Google Chrome"
        tell front window's active tab to set infoGrab to execute javascript "document.getElementsByClassName('even')[2].innerHTML;"
      end tell
    
      set theText to Unicode text
      set theSource to infoGrab
      set leftEdge72 to "<a href=\"/"
      set rightEdge72 to "\">"
    
      set saveTID to text item delimiters
      set text item delimiters to leftEdge72
      set classValue to text item 2 of theSource
      set text item delimiters to rightEdge72
      set uniqueIDKey to text item 1 of classValue
      set text item delimiters to saveTID
      return uniqueIDKey
    end getID