AutoHotkey Tip of the Week: Capture Web Page Addresses (URLs)

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

GetActiveBrowserURLMsgBoxThe 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 thiscapture 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:

  1. Add required script elements needed for the GetActiveBrowserURL() function.
  2. Include the GetActiveBrowserURL() function in the script or one of your function libraries.
  3. Capture the window title.
  4. 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
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:

  1. AutoHotkey grabs the window title using the WinGetActiveTitle command.
  2. The WinGetClass command captures Class of the active window—also required for the GetActiveBrowserURL() function but only just before calling it.
  3. The GetActiveBrowserURL() function call saves the Web page address to the variable sURL.
  4. CopyRefURLIf 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.
  5. 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 put the GetActiveBrowserURL() function directly into the script, use the #Include directive or place it in one of your script function libraries.

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

Library BenefitsWhile 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:

  1. Clicking the address bar to select the URL.
  2. 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!

10 thoughts on “AutoHotkey Tip of the Week: Capture Web Page Addresses (URLs)

  1. I have the feeling your usage of GetActiveBrowserURL() with extracting of the 2 lines with the variables ModernBrowsers and LegacyBrowsers and WinGetClass, sClass, A are not necessary. Why don’t you call the function as is?


  2. I have learned so much from your blog, Jack, thank you! I’m still a noob but usually I can follow along with your blog posts, and get the overall gist of things.. but you lost me on this one.

    It sounds like you are starting with code from a forum post (a function) and combining it with the standard clipboard routine (which normally I understand)– but this is where I get my confused & my brain gives up. I will try again tomorrow with a fresh set of eyes. Meanwhile, does anyone have a link where I can check out the final script? Or just an outline/overview of the sections? Thank you!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s