Kevin
Kevin

Reputation: 1106

AppleScript : if and repeat

I created a script to check the status of a web search

I added a repeat statement so the script can continue only if the search is completed or if the "SN" is invalid.

repeat 20 times
    set theSearchstate to "not ready"
    set checkIfSNIsCorrect to ""
    set checkIfSNIsInvalid to ""
    try
        tell application "Google Chrome"
            tell front window's active tab to set checkIfSNIsInvalid to execute javascript "document.getElementsByClassName('modal-body ng-scope')[0].innerHTML;"
            ## Invalid Serial 
            tell front window's active tab to set checkIfSNIsCorrect to execute javascript "document.getElementsByClassName('subheader ng-binding')[0].innerHTML;"
            ## SN and device ID 

        end tell



        if theSearchstate is equal to "not ready" then
            delay 1


        else if checkIfSNIsCorrect contains serialNumber then
            set theSearchstate to "Completed"
            set checkIfSNIsCorrect to "SN is Correct"
            exit repeat

        else if checkIfSNIsInvalid contains "Serial Does Not Exists" then
            set theSearchstate to "invalid S/N"
            exit repeat

        else if checkIfSNIsInvalid contains "serviceErrorWhileSearching" then
            set theSearchstate to "Error with GCRM"
            exit repeat
        end if



    on error
        --
    end try
end repeat

return theSearchstate

However this is not working at all, I tried in different way, but I can't make it work.

Any suggestion ?

Upvotes: 1

Views: 999

Answers (1)

JMichaelTX
JMichaelTX

Reputation: 1817

The variable "serialNumber" is NOT defined:

else if checkIfSNIsCorrect contains serialNumber then

This statement will always return true, so none of the other statements will be executed:

if theSearchstate is equal to "not ready" then

I Revised Your Script

Please test and advise if it works for you.
Be sure to set the serialNumber variable to the correct value.

--- MOVE VARIABLE INIT OUTSIDE OF REPEAT ---
set theSearchstate to "not ready"
set checkIfSNIsCorrect to ""
set checkIfSNIsInvalid to ""

--- ADD THIS ---
set serialNumber to "YourSerialNumber"  ## CHANGE to desired value

try

  repeat 20 times

    tell application "Google Chrome"
      tell front window's active tab to set checkIfSNIsInvalid to ¬
        execute javascript "document.getElementsByClassName('modal-body ng-scope')[0].innerHTML;"
      ## Invalid Serial 
      tell front window's active tab to set checkIfSNIsCorrect to ¬
        execute javascript "document.getElementsByClassName('subheader ng-binding')[0].innerHTML;"
      ## SN and device ID 

    end tell

    ### REMOVED if test for "theSearchstate"

    if checkIfSNIsCorrect contains serialNumber then
      set theSearchstate to "Completed"
      set checkIfSNIsCorrect to "SN is Correct"
      exit repeat

    else if checkIfSNIsInvalid contains "Serial Does Not Exists" then
      set theSearchstate to "invalid S/N"
      exit repeat

    else if checkIfSNIsInvalid contains "serviceErrorWhileSearching" then
      set theSearchstate to "Error with GCRM"
      exit repeat
    end if

    --- MOVE DELAY TO HERE ---
    --- NONE OF THE ABOVE MATCHED, SO DELAY & TRY AGAIN ---
    delay 1

  end repeat

on error errMsg number errNum

  set msgStr to "[ERROR]" & linefeed & errNum & ": " & errMsg
  set titleStr to "Check for Serial Number"

  display dialog msgStr ¬
    with title titleStr ¬
    with icon stop

  set buttonStr to button returned of result
  set theSearchstate to msgStr

end try

return theSearchstate

Upvotes: 2

Related Questions