A Trick for Creating a New Hotkey from a Subroutine (AutoHotkey Quick Tip)

Use the GoTo Command to Turn the Subroutine for Reading AHK File Notes into a Hotkey for Peeking into Any File Selected in Windows File Explorer

Last time in my “Peeking at Notes Inside Auto-Startup AHK Script Files (AutoHotkey Startup Control)” blog, I discussed the feature in the AutoStartupControl.ahk script for reading and displaying notes from a .ahk source file. The ScriptNotes subroutine uses the data in the Startup folder shortcut to locate the file, then parses out the first set of notes for display. This creates a quick reminder for the purpose and operation of the AutoHotkey scripts automatically loaded when Windows boots.

While a handy tool for the AutoStartupControl.ahk script, the subroutine would offer even more value if I could select any .ahk file in Windows File Explorer and read its notes. For that expanded capability, I need a new Hotkey combination that bypasses reading the Startup folder shortcut and directly accesses the file selected in the Explorer window.

You’ll find many varied methods for achieving this desired result ranging from writing a completely new Hotkey subroutine (possibly including functions) down to the quick-and-dirty trick I use here. Rather than make it too complicated, I opted to use the current subroutine with the addition of:

  1. The new Hotkey combination
  2. A new Label name for bypassing the FileGetShortcut command
  3. A GoTo command for jumping directly to that new Label

I don’t claim that this approach provides the best solution, but it does take advantage of the uniqueness of the lowly regarded GoTo command. The first part of the Hotkey subroutine includes the Standard AutoHotkey Windows Clipboard Routine:

   Clip0 := ClipboardAll        ; Backup current clipboard's content
   Clipboard :=                 ; Clear clipboard
   SendInput, ^c                ; copy selected file's path to clipboard
   ClipWait 0
   If FileExist(Clipboard)
      Location := Clipboard
      MsgBox, No file selected!
      Clipboard := Clip0           ; Restore original ClipBoard
   Clipboard := Clip0           ; Restore original ClipBoard

   Goto ReadNotes	

	FileGetShortcut, %A_Startup%/%A_ThisMenu%, Location

	SplitPath, Location, Name, Dir, Ext
		, Name_no_ext, Drive
	If InStr(Location,".exe")
		Location := StrReplace(Location, ".exe" , ".ahk")
	If !FileExist(Location)
		MsgBox No notes found!

	FileRead, FileVar, % Location
	found := RegExMatch(FileVar, "s)/\*(.*?)\*/" 
		, NotesVar)

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

Note: You’ll find the Standard AutoHotkey Windows Clipboard Routine (the first highlighted block above, lines 2 through 16) discussed in many of my blogs and AutoHotkey books.

The beginning of the CTRL+ALT+4 Hotkey subroutine uses standard code for the Clipboard routine except that it traps for the existence of the selected file (If FileExist(Clipboard)) before proceeding. This forces the selection to use a valid file path and name—either from Windows File Explorer or another source. The Clipboard routine ends before the GoTo statement in line 18.

Remember: Label names appear invisible to a running AutoHotkey script and have no effect when encountered during processing. They only get noticed when called by another AutoHotkey statement such as GoSub, GoTo, a GUI g-Label, or other AutoHotkey commands.

At this point, we want to skip past the FileGetShortcut command line and move directly to the remainder of the subroutine. By adding the new Label name ReadNotes: at line 23 and jumping directly to that location using the GoTo command, AutoHotkey runs the remainder of the subroutine without complication.

Since a GoTo statement does not return to the source of the crime, the prior code can appear anywhere in the script file. However, placing the snippet just before the remaining portion of the subroutine makes it easier to understand the action and locate the source.

Now, whenever I select the .ahk file (or any .exe with an accompanying .ahk file in the same folder) in Windows File Explorer, then use the CTRL+WIN+4 Hotkey combination, the script displays the first set of block comments (if any) found in the file in a GUI pop-up window similar to the following:

Note: In “Chapter Seven: Understanding Label Names and Subroutines” and “Chapter Eight: GoTo Command Versus GoSub Command” of my book Beginning Tips for Writing AutoHotkey Scripts, I clarify some of the nuances of AutoHotkey code found in this blog.

As you can see by the last few lines of the script, not only do I continue to use a GUI pop-up window for display purposes, but I’ve switched to the Link GUI control. This allows me to turn any URLs in the notes into hotlinks—making it easier to investigate the comments in the notes. Next time, I’ll talk more about the Link GUI control and the RegEx for creating the hotlinks for the GUI window.

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!

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

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