When Browsing the Web This Special Function Copies the Page URL without Any Extra Effort
Normally, you can find a Web page address in the URL bar at the top of the browser. Click in that address field and copy it with CTRL+C. That simple act may make the subject of this blog look redundant. However, when applied to last week’s CopyRegTagWin.ahk script, the GetActiveBrowserURL() function can save numerous keystrokes.
By using the user-defined GetActiveBrowserURL() function, a modified version of the CopyRefTagWin.ahk script can include both the title of the source window and, if from a Web page, its URL. If collecting data for research, this feature makes reconstructing sources much easier—without any extra effort.
The GetActiveBrowserURL() Function
The AutoHotkey Forum post “Get the URL of the current (active) browser tab” by atnbueno contains the GetActiveBrowserURL() function which grabs the URL for a Web page in a variety of browsers. It appears to work by identifying the browser then taking appropriate steps to copy the address. (It uses umpteen DllCall() function calls to get the job done.) Activate the target Web page and hit Hotkey combination CTRL+SHIFT+ALT+U (
^+!u) to extract the information.
After saving the Web page URL to the variable sURL, a modified CopyRefTarWin.ahk script (discussed in “Channeling Text to a Tagged Window“) makes sending the information to the target window easy. Adding this “capture Web page address” feature—as well as, a window title copy technique—to the new CopyRefTarWinURL.ahk script only requires a minimum number of changes.
Saving the Web Page Address and Window Title
When copying reference information to a tagged window, it makes sense to save the source title and URL (if any) with the selected text section. The modification requires the following steps:
- Add required script elements needed for the GetActiveBrowserURL() function.
- Include the GetActiveBrowserURL() function in the script or one of your function libraries.
- Capture the window title.
- Send the extracted Web address and window title to the target page.
Required Elements for GetActiveBrowserURL() Function
Two required AutoHotkey statements appear before the Hotkey definition in the posted GetActiveBrowserURL() function script:
ModernBrowsers := "ApplicationFrameWindow,Chrome_WidgetWin_0 ,Chrome_WidgetWin_1,Maxthon3Cls_MainFrm ,MozillaWindowClass,Slimjet_WidgetWin_1" LegacyBrowsers := "IEFrame,OperaWindowClass"
These variable definitions contain the lists of supported browser classes and must execute before using the function itself. Place them in the auto-execute section at the top of the script.
Add the GetActiveBrowserURL() function call to the script at the appropriate location inside the Standard Clipboard Routine:
WinGetActiveTitle, RefTitle WinGetClass, sClass, A sURL := GetActiveBrowserURL() If (sURL != "") MsgBox,3,, % "Title: " . RefTitle . "`nThe URL is " . sURL . "`rCopy URL to Ref? (Y/N)" IfMsgBox Yes Control, EditPaste, `; %sURL%`r`r,%control%, ahk_id %WinTag% Control, EditPaste, %Clipboard% `n`n ,%control%, ahk_id %WinTag%
The code above appears in the new script just before the original Control, EditPaste command (in green) which copies the selected text to the target window:
- AutoHotkey grabs the window title using the WinGetActiveTitle command.
- The WinGetClass command captures Class of the active window—also required for the GetActiveBrowserURL() function but only just before calling it.
- The GetActiveBrowserURL() function call saves the Web page address to the variable sURL.
- If the function finds a Web address, a MsgBox command displays the URL and offers the option to channel it to the target page. I would consider removing the MsgBox command and automatically sending the URL whenever found.
- For users copying code to an AutoHotkey test window, I added a semicolon in front of the URL to turn it into an AutoHotkey comment.
Include the GetActiveBrowserURL() Function
You can copy the complete function from the original AutoHotkey Forum post. I’ve also included it in the posted CopyRefTagWinURL.ahk script (at the end of the CopyRef.ahk section). Be sure to include all associated functions called by the original.
The Window Title
As shown in the code above, the script captures the window title using the WinGetActiveTitle command. I added the following code to always send the referenced window title to the tagged window:
Control, EditPaste, `; Reference Title: %RefTitle%`r ,%control%, ahk_id %WinTag%
This code automatically includes the window title with the copied reference txt. I preceded the title with a semicolon (
;) to make it invisible to running AutoHotkey code. The backtick mark (
`) escapes the special AutoHotkey comment properties of the semicolon.
The Application of the GetActiveBrowserURL() Function
While I had no problem using this GetActiveBrowserURL technique with the Chrome browser, a perusal of the forum shows a number of issues brought up by other users. It looks like it should work for the most commonly utilized programs. But, even with that, you may ask, “Why do I need it? The URL readily appears in the browser address bar.”
Maybe using this approach only saves you a couple of steps:
- Clicking the address bar to select the URL.
- CTRL+C or a Hotkey combination to capture the text.
But those two steps repeated a multitude of times add up to a mundane process. Plus, if you plan to create new controls such as a GUI Link for building a specialized Web page history pop-up window or save them to a file, you will find the data manipulation easier.
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!