20090407

macbook wireless refresh applescript

I regularly use a wireless router which does not play nice with macbooks. A solution for some of these issues could be an update of the system or some problem with the configuration of the router. However, that is not the case for the network in question. A dirty but quick solution has been turning wireless off and back on forcing a DHCP broadcast which revives the connection. Here are two scripts which simulate this action whenever the connection is down. NOTE: you will need to have a Network profile called "Ethernet only" on your system and you will need to change the routers IP in the script (192.168.2.1) to whatever IP your wireless router uses. Also, I use Growl to let the user know when a refresh happens. Remove this part of the code if you do not want to install growl (its free). Again, both of these scripts do the same thing but one is run from a terminal and the other, the Applescript, you can just double click on to run.

Applescript
repeat
try
set ping_result to (do shell script "ping -t 2 -c 1 192.168.2.1")
--display dialog ping_result buttons "OK" default button 1 with icon 2
on error errStatement number errNum --errStatement holds text of error message, errNum the integer code described in either 'man ping' or sysexits.h
if (errNum = 2) then
tell application "GrowlHelperApp"
set the allNotificationsList to {"Network Check"}
set the enabledNotificationsList to {"Network Check"}
register as application "Network Check" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Network Check"
notify with name "Network Check" title "Network Check" description "Wireless is down. Will try to restart" application name "Network Check"
end tell

do shell script "scselect \"Ethernet only\">/dev/null && scselect 'Automatic'>/dev/null"

tell application "GrowlHelperApp"
set the allNotificationsList to {"Network Check"}
set the enabledNotificationsList to {"Network Check"}
register as application "Network Check" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Network Check"
notify with name "Network Check" title "Network Check" description "Restart complete. Internet should revive itself any moment." application name "Network Check"
end tell

delay 60
end if
end try
delay 2
end repeat

Shell script
#!/bin/sh
while sleep 1; do
if ping -n -o -c 1 -t 1 192.168.2.1>>/dev/null; then
sleep 1
else
echo restarting `date`
osascript -e 'tell application "GrowlHelperApp" ' \
-e ' set the allNotificationsList to {"Network Check"} ' \
-e ' set the enabledNotificationsList to {"Network Check"} ' \
-e ' register as application "Network Check" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Network Check" ' \
-e ' notify with name "Network Check" title "Network Check" description "Wireless is down. Will try to restart" application name "Network Check" ' \
-e 'end tell'
scselect "Ethernet only" && scselect "Automatic"
osascript -e 'tell application "GrowlHelperApp" ' \
-e ' set the allNotificationsList to {"Network Check"} ' \
-e ' set the enabledNotificationsList to {"Network Check"} ' \
-e ' register as application "Network Check" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Network Check" ' \
-e ' notify with name "Network Check" title "Network Check" description "Restart complete. Internet should revive itself any moment." application name "Network Check" ' \
-e 'end tell'
sleep 65
echo wakeup
fi
done

A more desirable route would be to simply renew a DHCP lease but the anchors for this in applescript are not available and using the ipconfig shell command requires administrative privileges.