使用 Applescript/Automator 检查活动的互联网连接 [英] Check for active internet connection with Applescript/Automator

查看:33
本文介绍了使用 Applescript/Automator 检查活动的互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Automator 工作流程来 ping 服务器,并下载我经常使用的时间表的最新副本.然后将此时间表复制到我的保管箱中,以便我可以在手机上查看.在工作流下载最新计划之前,它会从 Dropbox 中删除旧计划.

I have an Automator workflow to ping a server, and download the latest copy of a schedule that I frequently use. This schedule then is copied to my dropbox so I can view it on my phone. Before the workflow downloads the newest schedule it deletes the old schedule from dropbox.

这很好用,除非我没有有效的互联网连接.当我没有有效的互联网连接时,工作流程仍会打开保管箱,删除旧计划,并尝试下载最新计划.因为没有连接,它不会下载任何东西.然后,如果我的连接处于活动状态,空的保管箱将同步,日程将从我的手机中删除.

This works well, except when I don't have an active internet connection. When I don't have an active internet connection, the workflow will still open up dropbox, delete the old schedule, and try to download the newest one. Because there is no connection, it doesn't download anything. Then if my connection becomes active the empty dropbox will sync and the schedule will be deleted from my phone.

我正在尝试添加几行 AppleScript 代码来 ping 服务器以查看是否有活动连接.如果我不这样做,请等待大约 5 秒钟,然后再次 ping.我想进行 5 次 ping 尝试,如果我仍然没有活动连接,那么我想完全退出.

I'm trying to add a few lines of applescript code to ping a server to see if i have an active connection. If I don't, then wait about 5 seconds and ping again. I want to have 5 ping attempts and at that point if i still do not have an active connection then I want to quit entirely.

我对 applescript 很陌生,所以我一直在想如何处理来自命令的错误,在这种情况下,是 ping.如果命令ping -o www.apple.com"失败,请等待 5 秒钟,然后重试 ping.如果尝试失败 5 次,则完全退出.

I'm very new to applescript, so I'm getting hung up on how to handle an error from a command, in this case, the ping. If command "ping -o www.apple.com" fails, wait 5 seconds and retry the ping. If 5 failed attempts then quit entirely.

推荐答案

也许是这样?

repeat with i from 1 to 5
    try
        do shell script "ping -o www.apple.com"
        exit repeat
    on error
        delay 5
        beep
        if i = 5 then error number -128
    end try
end repeat
say "Connected"

这篇关于使用 Applescript/Automator 检查活动的互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆