Automatically change the IP
address of the osc_server
domain name in the hosts file AHK script.
Thank you, @Greenman (for the hosts idea) and @GeneralMidi (for the AHK OSC integration DLL link - posted here)
Now, if I connect my O-S-C server
device and my client
device to another router, all I have to do is run the script, and my bookmark (http://osc_server:8080
) will be automatically linked to the correct IP.
Hurray!
Autohotkey code
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
!z:: Reload
!a::
; Variables
serverComputerName := "aaa" ;
domainName := "osc_server"
; 1) new_ip variale
; (store the IP of the computer named "aaa" into the "%new_ip%" variable)
; Credit goes to mikeyww (https://www.autohotkey.com/boards/memberlist.php?mode=viewprofile&u=59977)
Clipboard =
RunWait, %ComSpec% /c ping -n 1 %serverComputerName% | clip,, Hide
ClipWait, 0
If ErrorLevel {
MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return
} Else RegExMatch(Clipboard, "Reply from \K[\d.]+", new_ip)
; 2) LineNum variable
; (store to a variable the line number in which "osc_server" is contained; this helps in finding which line should be replaced in the next step)
strToFind := %domainName%
Loop, Read, c:\Windows\System32\drivers\etc\hosts
{
IfInString, A_LoopReadLine, %strToFind%
{
LineNum := A_Index
}
}
; 3) Replace the line containing "osc_server"
newLineContent = %new_ip% %domainName%
Loop, Read, c:\Windows\System32\drivers\etc\hosts
{
If A_Index = %LineNum%
FileAppend, %newLineContent%`n, %A_Temp%\Temp.txt
Else
FileAppend, %A_LoopReadLine%`n, %A_Temp%\Temp.txt
}
FileMove, %A_Temp%\Temp.txt, c:\Windows\System32\drivers\etc\hosts, 1