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).
I have tested dropping the code from GetURL and GetURL1 into small test scripts and both work perfectly for me on Chrome but do not return any URL on Firefox. Not sure what may be at fault here, I haven't tried the other versions, though. Is it worth giving those a shot?
EDIT 1: I came back after having my computer hibernated and it started working on Firefox now 🤷♂️
EDIT 2: Worked for a few hours then stopped working, however hibernation fixed it again? Not sure why I'm getting this weird behavior.