AppleScript:如果和重复

AppleScript : if and repeat

我创建了一个脚本来检查网络搜索的状态

我添加了一个 repeat 语句,因此只有在搜索完成或 "SN" 无效时脚本才能继续。

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

然而这根本不起作用,我尝试了不同的方法,但我无法让它起作用。

有什么建议吗?

未定义变量 "serialNumber":

else if checkIfSNIsCorrect contains serialNumber then

这条语句将始终return true,所以 none 的其他语句将被执行:

if theSearchstate is equal to "not ready" then

我修改了你的剧本

请测试并告知它是否适合您。
请务必将 serialNumber 变量设置为正确的值。

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