While Fixing the IPFind.ahk Script for Listing the Geographic Location of an IP Address, I Added Links for the IP Identification Site and OpenStreetMap
Occasionally, Web page scraping apps fail (or display strange results) due to changes in source page data formats. It usually only takes a few minutes to review the code and make the necessary RegEx adjustments to restore acceptable results. This time while repairing the IPFind.ahk script, I noticed that the Web page source code also offered IP longitude and latitude. I thought, “Why not add a map link to the display window for anyone curious about its geographic position?” The IP site (which I also added as a link) includes a map, but I wanted one with greater detail.

Note: I recently discussed the Link GUI control in “Turn Web Addresses into Hotlinks for the AHK File Peek Window.”
After loading, the IPFind.ahk script activates with the CTRL+ALT+I Hotkey combination, then extracts the IP (or group of IPs) from the selected text in any Windows program. Next, the script looks up each IP on the Web (without using a Web browser), extracting and displaying the location(s) as shown in the image at the right. You’ll find these techniques (along with other RegEx applications) using the RegExMatch() and RegExReplace() AutoHotkey functions discussed in the e-book A Beginner’s Guide to Using Regular Expressions in AutoHotkey.
To make the necessary changes, I needed to give up the simplicity of a MsgBox window and convert to a GUI (Graphical User Interface) window including Link GUI controls—which support hot HTML Web links. I often use a MsgBox window for quick looks and debugging code, but once I get serious, a GUI window offers much greater flexibility for both a practical application and text formatting.
In addition to the Link GUI control, I used Text controls and one Button control. The Text control does a better job of formatting without the foibles associated with text in a Link control. The Button control provides an obvious method for closing the window—although clicking the X
in the upper right-hand corner of the window works just as well.
I updated the old Clipboard routine to the more robust error handling that I now recommend. I also made the window always-on-top to force myself to close it when done. I find it easier to close an always-on-top window when I no longer need it than undertaking a search for a window suddenly hidden behind others.
The New GUI Window
To force the GUI window to remain always-on-top, the script issues the following command prior to adding any GUI controls:
Gui, +AlwaysOnTop
Incremented by the number of IPs found, AutoHotkey places the Text and Link controls in the new window using the Loop command. To implement the new GUI window, I moved the code outside of the GetLocation() function into the main section of the script:
Loop, %CountIP%
{
CheckIP := ipaddress%A_Index%
WhereIs := GetLocation(CheckIP)
WhereIs[1] := StrReplace(WhereIs[1],"State/Region","State")
WhereIs[1] := StrReplace(WhereIs[1],"Country:","`rCountry:`t")
WhereIs[1] := StrReplace(WhereIs[1],"State:","`rState:`t")
WhereIs[1] := StrReplace(WhereIs[1],"City:","`rCity:`t")
Gui, Add , Text,, % CheckIP . WhereIs[1]
Gui, Add , Link, yp+55, % "<a href=""https://whatismyipaddress.com/ip/" . CheckIp . """>IP Data" . "</a>"
Gui, Add , Link, yp+15, % WhereIs[2]
}
Gui, Add, Button,, Close
Gui, Show,,IP Locations
The GetLocation(CheckIP)
function captures data from the IP lookup Web page, then returns an array containing the location text and geographic coordinates [location text,longitude/latitude]
.
After formatting the text in the first element of the array returned from the GetLocation() function (WhereIs[1]
), the Gui, Add lines 9 through 11 add the geographic location, the site link, and the map link (WhereIs[2]
), respectively.
Tip: GUI windows offer exact placement of controls as demonstrated by the yp+55
and yp+1
5 options appearing in the Link controls—removing any awkward gap between the Text control and the two Link controls. For more in-depth information about how the GUI control formatting options work—including a chart demonstrating what each option does—see “Chapter Thirty-five: Text Formatting in GUIs” of Digging Deeper Into AutoHotkey.
After exiting the Loop, the GUI finishes off by adding a Close button (calling the ButtonClose:
subroutine which deactivates—then removes—the window without exiting the app) and the Gui,Show
command to activate the GUI window.
Extracting Longitude and Latitude
To find the data targeted for extraction, I usually search the source code page (right-click on the open Web page and select View page source to inspect) for keywords—in this case, “longitude” or “latitude” buried somewhere in the code. Next, I scour the code for the appropriate data.
Once located, I use the surrounding unique characters to build the RegEx:
A new Regular Expressions (RegEx) pulls the longitude and latitude from the site source code:
RegExMatch(version, "circle\(\[(.*?)\]", Map)
Map := RegExReplace(map1,"(.*),(.*)","$1/$2")
The wildcard RegEx expression (.*?)
saves the target text embedded between the unique keys to the variable Map.
Note: The open parenthesis and square brackets required escaping with a backslash ( \
) since both have special meaning in a Regex.
I used the RegExReplace() function to replace the separating comma with a slash matching requirements for OpenStreetMap URLs. If you decide to use Google Maps, you won’t need this adjustment. (See the e-book A Beginner’s Guide to Using Regular Expressions in AutoHotkey for more information about how to take advantage of RegEx.)
Formatting the Map and IP Data Links
This script uses two different methods for formatting the links. The script creates the Map link inside the GetLocation() function before returning it to the calling function:
Map := "<a href=""https://www.openstreetmap.org/#map=13/"
. Map . """>Map" . "</a>"
The GUI IP Data Web link results from a forced expression ( %
) inside the Gui, Add, Link command—directly adding the HTML code:
Gui, Add , Link, yp+55
, % "<a href=""https://whatismyipaddress.com/ip/"
. CheckIp . """>IP Data" . "</a>"
The above lines use line continuation techniques to wrap each line of code for display purposes.
Returning Multiple Values from a Function
You’ll find a number of different methods for returning multiples values from a function, but I chose a simple array to simultaneously assign both the location text and map link.
GetLocation(findip)
{
IPsearch := "https://whatismyipaddress.com/ip/" . findip
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", IPsearch)
whr.Send()
sleep 100
version := whr.ResponseText
RegExMatch(version, "Continent(.*)<div class=""right"">", Location)
RegExMatch(version, "circle\(\[(.*?)\]", Map)
Map := RegExReplace(map1,"(.*),(.*)","$1/$2")
Map := "<a href=""https://www.openstreetmap.org/#map=13/" . Map . """>Map" . "</a>"
Location := RegExReplace(Location1,"<.+?>")
Return [Location,Map]
}
Line 14 shows the format for sending a simple array of values back to the calling function. Once returned, the script must use array element format (i.e. WhereIs[1]
and WhereIs[2]
) to access the results.
The Close Button
When you close a MsgBox window, it goes away. Not so with a GUI window—unless you Destroy it. That means a GUI window requires extra code for removing it before rebuilding the GUI with new data. Otherwise, any Hotkey activation merely displays the last GUI.
If included in the script, the GuiClose:
subroutine executes whenever the user closes the window—whether by clicking the X
in the upper right-hand corner or issuing the WinClose command.
GuiClose:
Gui, Destroy
Return
ButtonClose:
WinClose
Return
Although the GUI window doesn’t require a Close button, the code demonstrates how a button name automatically creates the name of a subroutine (ButtonClose:
)—not requiring the g-label option.
To view the entire script, click IPFindMap.ahk at the InFind.ahk section of the “ComputorEdge Free AutoHotkey Scripts” Web page.
Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)
Find my AutoHotkey books at ComputorEdge E-Books!
Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!
[…] window rather than a MsgBox command. This upgrade facilitated adding links to the app, see “Adding Web Links to the AutoHotkey IPFind.ahk Script,” as well as making the current insertion of interactive maps using the ActiveX GUI control […]
LikeLike