AppleScript ping shell脚本始终返回true

时间:2015-07-03 23:30:39

标签: bash shell applescript

我试图让这个AppleScript在主机开机或离线时提醒我。如果我将do shell script行更改为set connected to true / false,则一切正常,因此我知道其余代码可以正常工作。但do shell script行似乎总是回归真实。当我在终端中运行它时工作正常,但由于AppleScript中的某些原因它没有。即使我将主机设置为每次在终端中返回false的随机IP地址,do shell script每次都返回true。我从this回答中获得了shell脚本。

on run
    set oldconnected to false
    repeat
        set connected to do shell script "ping -o -t 5 My-Host.local >/dev/null && echo yes || echo no" as boolean

        if connected and not oldconnected then
            display notification "Device has connected"
        end if

        if not connected and oldconnected then
            display notification "Device has disconnected"
        end if

        set oldconnected to connected
        delay 5
    end repeat
end run

1 个答案:

答案 0 :(得分:1)

您在do呼叫周围缺少一些括号。替换:

set connected to do shell script "ping -o -t 5 My-Host.local >/dev/null && echo yes || echo no" as boolean

使用:

set connected to (do shell script "ping -o -t 5 My-Host.local >/dev/null && echo yes || echo no") as boolean
你应该好好去!