I wanted a script that when I plugged my laptop into a wired network it would disable my wireless card and when I unplugged my laptop from a wired network it would re-enable my wireless card. I found many solutions that either were not free or did not work exactly how I wanted it to. I am open to suggestions and if you use my code and tweak it please post what you did and why. Other than that feel free to do with it as you please. Also, if you have any questions as to how or why I did something feel free to ask.
EDIT: Updated the script to use the Registry instead of the clipboard.
EDIT: Fixed Registry to overwrite old values if they exist.
EDIT: Added a ZIP download that includes icons.
; Script created by ELDERON
; Last modified 9/5/12
; Tested on AHK_L
; Tested on English Windows 7 Ultimate x64
; Online Repo:
; http://www.autohotkey.com/community/viewtopic.php?f=2&t=90229
; Instructions:
; Name your Wired Connection to LAN
; Name your Wireless Connection to WIFI
; Configure this script to run when you login
; ex. Save as C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\ToggleWiFi.ahk
; ex. Add a registry entry under HKU\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Run
Menu, Tray, Icon, %A_ScriptDir%\Assets\NOWIFI.ico,,1
;STARTOFSCRIPT - Auto reload script if changed
SetTimer,UPDATEDSCRIPT,1000
; Checks every 10 seconds for change in LAN adapter status
Loop{
Menu, Tray, Tip, Checking LAN Connection! Tries: %A_Index% ; For Debug - Remove this line
Sleep, 10000
TOGGLEWIFI()
}
;FUNCTION
TOGGLEWIFI()
{
; Deletes old Interface information
RegDelete, HKLM, Software\AHKSCRIPTS\ToggleWiFi
; Collects interface information and saves to the registry under HKLM\Software\AHKSCRIPTS\ToggleWIFI
RunWait, %comspec% /c for /f "delims=" `%i in ('netsh interface show interface') do (if NOT "`%i"=="" (if NOT "`%i"=="Admin State State Type Interface Name" (if NOT "`%i"=="-------------------------------------------------------------------------" (reg add HKLM\Software\AHKSCRIPTS\ToggleWIFI /t REG_SZ /v "`%i" /d "`%i" /f)))), , hide
; Reads all interfaces and saves them to variable INTSTAT
Loop, HKLM, Software\AHKSCRIPTS\ToggleWIFI, 0, 0
{
RegRead, value
if ErrorLevel
value = *error*
INTSTAT .= value . "-"
;MsgBox, 4, , %value%`n`nContinue?
;IfMsgBox, NO, break
}
; Removes beginning and ending dashes
INTSTAT := Trim(INTSTAT,"-")
; Replaces all double spaces with a single space
Loop
{
StringReplace, INTSTAT, INTSTAT, %A_Space%%A_Space%, %A_Space%, UseErrorLevel
if ErrorLevel = 0 ; No more replacements needed.
break
}
; Splits interfaces by a dash
StringSplit, INTERFACE, INTSTAT, -, , All
; Loops through each found interface until it finds LAN
Loop, %INTERFACE0%
{
INTNAME := INTERFACE%a_index%
if (InStr(INTNAME, "LAN")){
; If this is the LAN adapter split it into it's settings
StringSplit, LAN, INTERFACE%a_index%, %A_SPACE%
; Double check that this is the LAN adapter
if (LAN4 = "LAN") {
; Make sure it is enabled, if not this script is useless
if (LAN1 = "Enabled") {
; When an Ethernet cable is plugged in LAN2 should equal Connected
if (LAN2 = "Connected"){
; Loops through each found interface again until it finds WIFI
Loop, %INTERFACE0%
{
INTNAME := INTERFACE%a_index%
if (InStr(INTNAME, "WIFI")){
; If this is the WIFI adapter split it into it's settings
StringSplit, WIFI, INTERFACE%a_index%, %A_SPACE%
; Double check that this is the WIFI adapter
if (WIFI4 = "WIFI") {
; If it is already disabled don't do anything
if (WIFI1 = "Enabled") {
; Disables the WIFI adapter
runwait, %comspec% /c netsh interface set interface name="WIFI" admin=DISABLED, , hide
Menu, Tray, Icon, %A_ScriptDir%\Assets\NOWiFi.ico,,1
;MsgBox, WIFI should be DISABLED
}
}
}
}
; If there is no Ethernet cable connected this LAN2 should equal Disconnectd
}else if (LAN2 = "Disconnected"){
; Loop through each found interface again until it finds WIFI
Loop, %INTERFACE0%
{
INTNAME := INTERFACE%a_index%
if (InStr(INTNAME, "WIFI")){
; If this is the WIFI adapter split it into it's settings
StringSplit, WIFI, INTERFACE%a_index%, %A_SPACE%
; Double check that this is the WIFI adapter
if (WIFI4 = "WIFI") {
; If it is already enabled don't do anything
if (WIFI1 = "Disabled") {
; Enables the WIFI adapter
runwait, %comspec% /c netsh interface set interface name="WIFI" admin=ENABLED, , hide
Menu, Tray, Icon, %A_ScriptDir%\Assets\WiFi.ico,,1
;MsgBox, WIFI should be ENABLED
}
}
}
}
; If LAN is not found error out here
}else{
MsgBox, Something is wrong!`n*****Interface List*****`n%TRIM1%`n*****EOF*****`nMake sure your wired connection is named LAN and your wireless connection is named WIFI!
}
}
}
}
}
return
}
UPDATEDSCRIPT:
FileGetAttrib,attribs,%A_ScriptFullPath%
IfInString,attribs,A
{
FileSetAttrib,-A,%A_ScriptFullPath%
SplashTextOn,,,Updated script,
Sleep,500
Reload
}
Return
;ENDOFSCRIPT - Auto reload script if changed




