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).
Little over a month ago (late Feb) I simplified the function... it bit me in the ass.
Simplified version looked for either a
SYSTEM_TEXT
role for Firefox and all the other Chromium-based browsers and theSYSTEM_DOCUMENT
role for Chrome... but I guess in the later updates Google changed the way Assistive Technology is detected. So I'm gonna go back to the previous version of the function and just grab the Address bar and add the appropriate schema for Chrome.If I were to use the fix you linked I couldn't get the proper schema or I'd need to hardcode
https
which is fine for the most part but then there are times wherehttp
Is actually needed. Also even thoughftp://
is dead there are other protocols and totally forgot that I needed to take care of them.Anyway is "fixed" now, but I don't like the way it is... I liked the streamlined version of it, so hopefully I get some free time to look for the proper fix rather than a simple patch). Another point of failure is the fact that other Electron or Chrome Embedded Framework browsers that have the accessibility disabled by default will struggle so it need a proper fix.
And thanks a lot for looking into this!
On that topic I started reading the on the subject and I have a good idea what is wrong and how to fix it:
https://sites.google.com/a/chromium.org/dev/developers/design-documents/accessibility
I just need some free time , which is a luxury I currently don't have but as soon as I'm able I'll update it.