If statement AppleScript:if和repeat

If statement AppleScript:if和repeat,if-statement,applescript,repeat,If Statement,Applescript,Repeat,我创建了一个脚本来检查web搜索的状态 我添加了一个repeat语句,这样脚本只有在搜索完成或“SN”无效时才能继续 repeat 20 times set theSearchstate to "not ready" set checkIfSNIsCorrect to "" set checkIfSNIsInvalid to "" try tell application "Google Chrome" tell front

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

我添加了一个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”未定义:

否则,如果checkIfSNIsCorrect包含serialNumber,则

此语句将始终返回
true
,因此不会执行任何其他语句:

如果搜索状态等于“未就绪”,则

我修改了你的剧本 请测试并建议它是否适合您。
确保将
serialNumber
变量设置为正确的值

——将变量INIT移到REPEAT之外---
将搜索状态设置为“未就绪”
将checkIfSNIsCorrect设置为“”
将checkIfSNIsInvalid设置为“”
---加上这个---
将serialNumber设置为“YourSerialNumber”##更改为所需值
尝试
重复20次
告诉应用程序“谷歌浏览器”
告诉前窗口的活动选项卡将checkIfSNIsInvalid设置为
执行javascript“document.getElementsByClassName('modal-body ng scope')[0]。innerHTML
##无效序列号
告诉前窗口的活动选项卡将checkIfSNIsCorrect设置为
执行javascript“document.getElementsByClassName('subheader ng binding')[0]。innerHTML
##序列号和设备ID
结束语
###如果“搜索状态”的测试已删除
如果checkIfSNIsCorrect包含serialNumber,则
将搜索状态设置为“已完成”
将checkIfSNIsCorrect设置为“序列号正确”
退出重复
否则,如果checkIfSNIsInvalid包含“序列不存在”,则
将搜索状态设置为“无效序列号”
退出重复
否则,如果CheckIfsInValid包含“serviceErrorWhileSearching”,则
将搜索状态设置为“GCRM错误”
退出重复
如果结束
---移到这里---
---以上各项均不匹配,请延迟并重试---
延迟1
结束重复
关于错误errMsg number errNum
将msgStr设置为“[ERROR]”&linefeed&errNum&“:”&errMsg
将标题设置为“检查序列号”
显示对话框msgStr
标题为
带图标停止
将buttonStr设置为返回结果的按钮
将搜索状态设置为msgStr
结束尝试
返回执政状态

检查是否引发错误。在错误日志中,error end try有变量SerialNumber,但我没有添加整个脚本。这段代码更有意义,尽管在脚本继续之前,我有6秒的延迟,但这很好,谢谢。