How do I write an AppleScript which auto reconnects to Wi-Fi?
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