It retrieves the URL (protocol included) of any browser via either MSAA framework or UI Automation interfaces.
Testing made with the most used* browsers as of September 2023 (versions as of October 2023).
* Chrome, Edge, Firefox and Opera (market share above 1%).
It uses the WinTitle & Last Found Window mechanism, so it receives up to 4 parameters (all optional) as described in the WinExist() docs function.
Function signature:
url := GetUrl([WinTitle, WinText, ExcludeTitle, ExcludeText])- F1 will retrieve the URL from the active window.
- F2 will retrieve the URL of the last found window.
- F3 will retrieve the URL when the browser is not active.
#Requires AutoHotkey v2.0
F1:: {
url := GetUrl("A")
if (url) {
MsgBox(url, "Active window URL", 0x40040)
} else {
MsgBox("Couldn't retrieve an URL from the active window.", "Error", 0x40010)
}
}
F2:: {
WinExist("Mozilla Firefox")
url := GetUrl()
if (url) {
MsgBox(url, "Current URL in Firefox", 0x40040)
} else {
MsgBox("Couldn't retrieve Firefox URL.", "Error", 0x40010)
}
}
F3:: {
url := GetUrl("ahk_exe firefox.exe")
if (url) {
MsgBox(url, "Current URL in Firefox", 0x40040)
} else {
MsgBox("Couldn't retrieve Firefox URL.", "Error", 0x40010)
}
}Files marked with an asterisk work standalone (ie, don't require a library).
- GetUrl.ahk* - Active window only
- GetUrl1.ahk* - Active window only (v1.1)
- GetUrl_Acc.ahk - Uses MSAA
- GetUrl_Acc1.ahk - Uses MSAA (v1.1)
- GetUrl_UIA.ahk - Uses UIAutomation
- GetUrl_UIA1.ahk - Uses UIAutomation (v1.1)
Rename to GetUrl.ahk if using libraries of functions.
Links to Accessibility libraries:
- MSAA Lib (maintained by me) for AutoHotkey (v2.0/v1.1).
- UIAutomation Lib (maintained by thqby) for AutoHotkey v2.0.
- UIAutomation Lib (maintained by Descolada) for AutoHotkey v2.0 (and v1.1).
Perhaps I'm not the only one, but in Firefox 143.0b9, GetUrl_Acc.ahk seems to have broken where it used to work perfectly.
If the sidebar is open , it grabs that link ex: moz-extension://ec23731a-a096-4bd3-b5d8-61c3c2868d48/sidebar/sidebar.html or
chrome://browser/content/syncedtabs/sidebar.xhtml
The only time it doesn't grab the url from the sidebar is if the sidebar is open to History or Bookmarks.
Additionally if it does not grab the url from the sidebar, it is still broken as it does not get the url of the active tab but the url from a different tab (can't really pinpoint how it decides which tab yet but if that tab is closed it'll then grab the tab to the right of the closed tab).