Turn Web Addresses into Hotlinks for the AHK File Peek Window (AutoHotkey Tip)

Using the AutoHotkey GUI Link Control to Display AHK File Notes Allows You to Turn Web Links Hot

While perusing the notes in various .ahk scripts using the subroutine ReadNotes—which I had added to the AutoStartupControl.ahk script and discussed in my blog “Peeking at Notes Inside Auto-Startup AHK Script Files (AutoHotkey Startup Control)“—I noticed that many scripts included URLs to reference sites. A common practice used by scriptwriters when giving credit to another script or offering additional information about the source, these sites can offer valuable insight or resources. Usually, a Web address appears as a complete URL including the HTTP(S)://. I wondered, “Wouldn’t it be great to just click a link in the Notes window to load the page?”

Since we write AutoHotkey scripts in plain text, attempting to provide hotlinks inside the file using HTML code (or other techniques) doesn’t make much sense. I can open the file and copy the Web address—pasting it into my browser, but a hotlink in the Notes window would save a lot of time. I immediately switched from using the Text GUI control to the Link GUI control. By inserting the Link control into the AutoStartupControl Notes GUI window, I can turn any URL into a hotlink—as long as I use a Regular Expressions (RegEx).

The Link GUI control in the Notes window can turn any fully formed Web address into a hotlink for immediate access.

Using the Link GUI control comes with a couple of foibles, but, for the most part, it behaves in a manner very similar to the Text GUI control.

Creating Hotlinks in a GUI Window

The Link GUI control acts like the Text GUI control except you can add hotlinks to the accompanying text. While not tremendously flexible, the control makes formatting Web URLs easy.

Gui, Add, Link,, This is a <a href="https://www.autohotkey.com">link</a>

The Link control uses HTML tags to enclose the hot text (e.g. “link”). The <a> tag includes the HREF property which names the target URL (enclosed in double-quotes). The tag </a> terminates the highlighted link text. When clicked, the hotlink executes the AutoHotkey Run command using the HREF address. (That’s all the HTML you need to know!)

You’ll find it easy enough to embed a link directly in an AutoHotkey GUI script, but what if you don’t know whether or not any URLs will appear in the loaded text? You first need a method for identifying random URLs. Regular Expression (RegEx) wildcards provide the best method for finding and manipulating unknown pieces of text.

Regular Expressions in AutoHotkey

Using the RegExReplace() Function to Find and Format Web Addresses

The RegExReplace() function works in a manner similar to StrReplace() function except RegEx functions accept wildcard characters allowing AutoHotkey to match ambiguous text. While you could use StrReplace() to find and format Web hotlinks, the process would require many more steps and a couple of gymnastics. By using RegExReplace(), a simple expression does all of the work in one pass.

To develop the expression, I opened Ryan’s RegEx Tester and inserted sample script notes (including a Web address) into the RegExReplace tab:

As discussed in my book A Beginner’s Guide to Using Regular Expressions in AutoHotkey:
1. Insert the search text in the top pane.
2. Add the search characters and symbols to the Regular Expressions pane.
3. Add the Replacement Text expression.
4. View changes in real-time in the Results pane.

Playing around with Ryan’s RegEx Tester yielded the following RegExReplace() function:

NotesVar1 := RegExReplace(NotesVar1, "(http.*?)(\s)"
                        , "<a href=""$1"">$1</a>$2")

Note: The above snippet uses line continuation techniques to wrap the one-line code for display purposes.

The needle-in-the-haystack RegEx “(http.*?)(\s)” works for both HTTP and HTTPS type URLs. After finding any “http” in the selection, the expression matches all available characters .* until ? it encounters the next \s (space, tab, or end of line character).

By placing parentheses around the two parts of the expression, RegEx creates the subexpressions $1 and $2 for use as replacement parameters in the function:

$1 = (http.*?)
$2 = (\s)

The first subexpression $1 acts as both the HREF address and the highlighted text located between the HTML <a>…</a> tags. The second subexpression $2 merely replaces the \s detected by the RegEx at the end of the address.

The replace expression “<a href=""$1"">$1</a>$2” adds the HTML tags <a>…</a>, using $1 for both the HREF target, and the address appearing as the text between the tags.

Note: Although not required in Ryan’s RegEx Tester, inside the parameters for the RegExReplace() function, you must escape any double-quotation marks with a preceding double-quote when appearing in the expression. This prevents the function from interpreting the double-quote character as the beginning or end of a parameter.

The following lines of code update the AutoStartupControl.ahk script to create hotlinks from any URLs encountered in the selected text, then use the Link GUI control to display those hotlinks.

NotesVar1 := RegExReplace(NotesVar1, "(http.*?)(\s)", "<a href=""$1"">$1</a>$2")
NotesVar1 := StrReplace(NotesVar1, "`t" , "     ")
Gui, Add, Link,  , %Location%`r%NotesVar1%
Gui, Show,  , %Name_no_ext%

Note: Unlike the Text GUI control, the Link GUI control ignores tab characters when displaying text drawn from a variable. To account for this quirk, I added a StrReplace() function to replace all tabs (`t) with five spaces:

NotesVar1 := StrReplace(NotesVar1, "`t" , " ")

While the Link GUI control works pretty well, I ran into a problem when the auto-sized Notes window grew too big for the screen. Excessively long Notes sections cut off the comments. Other than using the Edit GUI control, I don’t know an easy way to add scroll bars to a Text or Link type GUI control. Next time, I’ll look at quick-and-dirty solutions for the long-text problem, as well as, other more complex possibilities.

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.)

jack

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!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

One thought on “Turn Web Addresses into Hotlinks for the AHK File Peek Window (AutoHotkey Tip)

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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