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).
Is the same that does the function you linked, which BTW doesn't work on Firefox (takes like 10 seconds to fail in which them the app is non-responsive because it goes trough all the accessible elements on each tab) and will absolutely won't work on other browsers unless you call
IUIAutomation::CreatePropertyCondition
with a handle per browser per language.And yeah, that's why I changed the function: to simplify it and to avoid language specific pitfalls...
I use a lot MSAA and is way easier to read/write code with it than enumerating interfaces through COM/pointers keeping tabs of object references and such.
If you don't want to include the whole Acc library you could always embed the DLL calls in the function and it'll remove the dependency but I fail to see the benefit of that... the function will look as cramped as the one in the forums.
In the end both do the exact same thing, they recursively walk the Accessibility Tree look until a match is found for the string... difference with MSAA is what is supported 100% within the Chromium code base unlike IUIAutomation which is the most limited and that you don't need to use a different
IUIAutomation::ElementFromHandle
depending on the address bar string.I guess that you can use something like
IUIAutomation::CreateOrConditionFromArray
in order to add as many strings as possible or even something other to grab the actual URL (element value) but I don't have any experience with those interfaces... ergo, I fixed my version and now tells Chrome there's Assistive Technology.The benefits is that it works on pretty much every browser and not just Chrome (cannot test more than a handful) and works with
http
/https
protocols and internal pages (likechrome://
,about:preferences
and such).Again, thanks a lot for the report and insights.