Skip to content

Instantly share code, notes, and snippets.

@cyphunk
Created February 13, 2012 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyphunk/1819416 to your computer and use it in GitHub Desktop.
Save cyphunk/1819416 to your computer and use it in GitHub Desktop.
Apple Wireless Refresh AppleScript
(* Wifikeep - Cycle a Apple Wireless connection. *)
-- Determine style of alert:
set useInvertScreen to true
set useBeepAlert to false
set useVoiceAlert to false
set useGrowlAlert to false -- only if you have growl installed
-- the following works for OSX 10.6.*:
set cyclewifi to "/usr/sbin/networksetup -setairportpower en1 off && /usr/sbin/networksetup -setairportpower en1 on"
-- Another option:
-- To use you need to first create a network profile called
-- 'Ethernet only' that does not have the wireless card. The
-- wireless enabled profile should be called 'Automatic'.
--set cyclewifi to "scselect 'Ethernet only'>/dev/null && sleep 1 && scselect 'Automatic'>/dev/null"
set router to (do shell script "netstat -nr | grep default | awk '{print $2}'")
do shell script cyclewifi
delay 5
repeat
try
do shell script "ping -t 2 -c 1 " & router & " 1>/dev/null"
on error errStatement number errNum
-- errStatement holds text of error message, errNum the integer
-- code described in either 'man ping' or sysexits.hif (errNum = 2)
if (errNum = 2) then
-- DIFFERENT WAYS TO ALERT
log "Network Down. Restarting."
invertScreenToggle()
beepAlert()
voiceAlert("Network Down")
growlAlert("Wireless is down. Will try to restart")
do shell script cyclewifi
growlAlert("Restart complete. Internet should revive itself any moment.")
invertScreenToggle()
delay 60
end if
end try
delay 2
end repeat
on invertScreenToggle()
global useInvertScreen
if not useInvertScreen then return
tell application "System Events" to key code 28 using {command down, control down, option down}
end invertScreenToggle
on growlAlert(message)
global useGrowlAlert
if not useGrowlAlert then return
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 message application name "Network Check"
end tell
end growlAlert
on beepAlert()
global useBeepAlert
if not useBeepAlert then return
beep 2
end beepAlert
on voiceAlert(message)
global useVoiceAlert
if not useVoiceAlert then return
say message
end voiceAlert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment