How do I write an AppleScript which auto reconnects to Wi-Fi? Sometimes I need to download very big files from the internet using torrents (I'm using Transmission) and the Wi-Fi always drops. I bought several routers and modems and none of them work. I've been having this issue a for a while (~10 years, with both Windows and Mac).

Now I decided to learn a bit, and write some AppleScript which reconnects automatically:

repeat
if (do shell script "curl --connect-timeout 1 &lt;WIFI_IP&gt;") = "curl: (7) couldn't connect to host" then
    do shell script "networksetup -setairportpower AirPort off"
    do shell script "networksetup -setairportpower AirPort on"
    do shell script "networksetup -setairportnetwork en1 &lt;ROUTER_NAME&gt; &lt;ROUTER_PASS&gt;"
end if
end repeat


the part after then works, it disconnects from Wi-Fi and reconnects it, but the problem is with the if part, because I don't know how to detect when the Wi-Fi connection is broken by the torrent. How should I do this?

Also, a repeat with an interval would be really nice, something like: repeat every 10 seconds.
Here's a simple Appplescript handler I wrote a while back to solve this exact problem. It pings the OpenDNS server (both of them for redundancy) to check if the internet connection is up. It returns true on success, false on failure. Just copy it into your script, and call it like:

set connected to checkInternetConnection


Then you can put whatever you want to do in an if statement like:

if connected then
    -- Do stuff here
end if


The handler:

on checkInternetConnection()
    -- Ping the primary OpenDNS server.
    try
        set pingResult1 to do shell script "ping -c 1 208.67.222.222"
    on error
        set pingResult1 to ""
    end try

    -- Check the results returned and return true or false.
    set p to number of paragraphs in pingResult1
    if p &lt; 5 then
        -- Ping another Open DNS server for redundancy.
        try
            set pingResult2 to do shell script "ping -c 1 208.67.220.2220"
        on error
            set pingResult2 to ""
        end try

        set p to number of paragraphs in pingResult2
        if p &lt; 5 then return false
    else
        return true
    end if
end checkInternetConnection