AppleScript ping shell 脚本总是 returns 真
AppleScript ping shell script always returns true
我正在尝试让这个 AppleScript 在主机打开或离线时提醒我。如果我将 do shell script
行更改为 set connected to true
/false
,一切正常,所以我知道其余代码有效。但是 do shell script
行似乎总是 return 正确。当我 运行 它在终端中时它工作正常,但由于某种原因在 AppleScript 中它没有。即使我将主机设置为随机 IP 地址,每次在终端中 returns false,do shell script
returns 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
您在 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
你应该可以开始了!
我正在尝试让这个 AppleScript 在主机打开或离线时提醒我。如果我将 do shell script
行更改为 set connected to true
/false
,一切正常,所以我知道其余代码有效。但是 do shell script
行似乎总是 return 正确。当我 运行 它在终端中时它工作正常,但由于某种原因在 AppleScript 中它没有。即使我将主机设置为随机 IP 地址,每次在终端中 returns false,do shell script
returns 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
您在 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
你应该可以开始了!