Applescript如果问题

时间:2015-04-04 12:07:25

标签: macos applescript wake-on-lan

嘿伙计们,我的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

现在我不知道如何使用on ping得到的错误命令循环

2 个答案:

答案 0 :(得分:0)

即使你自己找到了解决方案,也没有答案,所以我试着在这里完成。当我们将两个字符串放在彼此的顶部时,字符串我们将看到" 100。 0%丢包"。正如您在寻找" 0%数据包丢失时所看到的那样#34;当您只发送1个数据包时,它总是会返回true。为了更安全而不是遗憾,您可以更好地匹配整条线路,传输1个数据包,接收1个数据包,丢失0.0%的数据包#34;你的第一个解决方案将通过更改匹配来解决(如果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的打印字符串不匹配但匹配返回的数字并将其强制转换为布尔值。

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

答案 1 :(得分:0)

现在的工作脚本:

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