Applescript if while 问题

Apple script if while issiue

大家好,我的 Applescript 没有达到我的预期。 我的错误在哪里?

try

set NAS to do shell script "ping -c 1 192.168.222.5"
if NAS contains "100.0% packet loss" then repeat until NAS contains "0% packet loss"
    do shell script "python /Users/Selim/Desktop/wol2.0.sh"
    set NAS to do shell script "ping -c 1 192.168.222.5"
end repeat
delay 20
tell application "Terminal" to activate
end try

我想 ping 到我的 NAS,当我没有收到响应时,我想叫醒他,但是我的脚本在发送 ping 而没有响应后停止。 知道我做错了什么吗? 我想发送数据包直到NAS唤醒。

我修改了密码

try
do shell script "ping -c 1 192.168.222.5"
on error
set NAS to "100% packet loss"
repeat while NAS contains "100% packet loss"
    do shell script "python /Users/Selim/Desktop/wol2.0.sh"
    set NAS to do shell script "ping -c 1 192.168.222.5"
    if NAS contains "0% packet loss" then exit repeat

end repeat
say "Server startet, 20 sek"
delay 20
end try

tell application "Terminal" to activate

现在我不知道如何让循环与从 ping

获得的 on error 命令一起工作

即使您自己找到了解决方案,这里也没有答案,所以我尝试在这里完成。当我们将两个字符串放在彼此之上时的字符串,我们将看到“100.0% packet loss”。正如您在查找“0% 数据包丢失”时所看到的,当您仅发送 1 个数据包时,它总是 return 为真。为了比抱歉更安全,你最好匹配整行“1 个数据包传输,1 个数据包接收,0.0% 数据包丢失”。您的第一个解决方案将通过更改匹配项来解决(您也可以删除 if 语句,如果 ping 第一次成功,则重复不会 运行 一次):

set NAS to do shell script "ping -c 1 192.168.222.5 || true"
repeat until NAS contains "1 packets transmitted, 1 packets received, 0.0% packet loss"
    do shell script "python /Users/Selim/Desktop/wol2.0.sh"
    set NAS to do shell script "ping -c 1 192.168.222.5 || true"
end repeat
delay 20
tell application "Terminal" to activate

更新:更好的版本不匹配 ping 的打印字符串,但匹配 returned 数字并将其强制转换为布尔值。

repeat until ping("192.168.222.5")
    do shell script "python /Users/Selim/Desktop/wol2.0.sh"
end repeat
delay 20
tell application "Terminal" to activate

on ping(IPNumber)
    return (do shell script "ping -t 1 -c 1 " & IPNumber & " >/dev/null && echo yes || echo no") as boolean
end ping

现在的工作脚本:

try
do shell script "ping -c 1 192.168.222.5"
on error

set NAS to "q"

repeat until NAS contains "1 packets transmitted, 1 packets received, 0.0% packet loss"
    do shell script "python /Users/Selim/Documents/scripte/wol2.0.sh"
    delay 0.1
    try
        set NAS to do shell script "ping -c 1 192.168.222.5"

    end try
end repeat

delay 5
end try

tell application "Kodi" to activate