Peeking at Notes Inside Auto-Startup AHK Script Files (AutoHotkey Startup Control)

We Can Track Numerous AutoHotkey Scripts Added to the Startup Folder, But Can We Remember How They All Work? Add a Peek Capability to the Auto-Startup Menu as an App Reminder

With so many different AutoHotkey scripts running, the problem of remembering how they all work arises. I may know that I have an app running but not recall the Hotkey combinations needed to access its features. Each new app creates a new set of memory challenges.

I could write one huge help message, but keeping it up-to-date turns into an unwieldy problem. I need a method for quickly peeking at an apps notes without forcing myself to open the .ahk file.

To accomplish this feat, the new ScriptNotes subroutine in the AutoStartupControl.ahk script must:

  1. Load the shortcut’s target file into a variable.
  2. Extract the script notes from that variable.
  3. Display the extracted notes in a pop-up MsgBox.

Note: The .ahk file must exist. If the shortcut targets an .exe compiled file, the script must then look for a corresponding .ahk file at the same location.

Tip: If no .ahk file exists for the .exe target, you might create a dummy .ahk file in the same folder as the .exe file containing helpful notes.

Tip: You can use this file peek technique for any text file—not just AutoHotkey script files. Merely, adjust the target file path and the Regular Expressions (RegEx) for the application to extract your desired text notes.

Add a Submenu

Last time, I added submenus to the AutoStartupControl.ahk script (“Add Submenus to the Auto-Startup Menu to Increase Options“). The script now builds a submenu for each shortcut found in the Windows Startup folder. I can add any number of submenu items using very little new code. That makes it easy to increase the capabilities of the script. I only need to insert another submenu item calling a new subroutine for each additional feature, then write the code for that subroutine—the hard part.

Each new feature requires one submenu item inserted with just one line of code:

Menu, [Submenu Name], Add, Notes, [Subroutine Label]

Since the Menu command creating the submenu resides inside the main loop, it adds the new menu item to each of the top-level menus:

AutoStart:
Loop %A_Startup%\*.*
{
	FileGetShortcut, %A_LoopFileFullPath%
		, Location
		, OutDir
		, OutArgs
		, OutDescription
		, OutIcon
		, OutIconNum
		, OutRunState
	If ErrorLevel
		Continue
	Menu, %A_LoopFileName%, Add, Notes, ScriptNotes
	Menu, %A_LoopFileName%, Add, Open, MenuAction
	Menu, %A_LoopFileName%, Add, Restart, ProgRestart
	Menu, Tray, Add , %A_LoopFileName%, :%A_LoopFileName%
	If (OutIcon != "")
		Menu, Tray, Icon
			, %A_LoopFileName%, %OutIcon%
			, %OutIconNum%
}
Menu, Tray, Icon, Shell32.dll, 85
Return

Line 14 adds the submenu item Notes to the submenu %A_LoopFileName% (the name of the shortcut as well as the top-level menu item). The subroutine Label ScriptNotes pops up a MsgBox displaying the parsed text extracted from the .ahk file.

The ScriptNotes Subroutine

Similar to the other subroutines in this AutoStartupControl.ahk script, the ScriptNotes subroutine extracts data from the Windows shortcut using the FileGetShortcut command. Next, it loads the contents of the target file (if .ahk extension) into a variable using the FileRead command. Once loaded, AutoHotkey parses the notes from the file variable using the RegExMatch() function. Finally, the script displays the parsed notes in a MsgBox.

ScriptNotes:
	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!
		Return
	}
	FileRead, FileVar, %Location%
	Found := RegExMatch(FileVar, "s)/\*(.*?)\*/" 
		, NotesVar)
	NotesVar := RegExReplace(NotesVar1
		, "(\S\s?)`r`n(\s?\S)" , "$1 $2")
	if NotesVar contains `n,`r,`t
	{
		NotesVar := StrReplace(NotesVar, "``n" , "`r`n")
		NotesVar := StrReplace(NotesVar, "``r" , "`r`n")
		NotesVar := StrReplace(NotesVar, "``t" , "`t")
		NotesVar := StrReplace(NotesVar, "`t," , ",")
	}
	MsgBox, 0, %Name_no_ext%, % Location "`r" NotesVar
Return

Note: The ScriptNotes subroutine extracts the text located between the first set of comments markets (/* and */).

You’ll find a number of issues worth discussing in this subroutine:

  1. Locating the .ahk file for reading the text notes.
  2. How to use Regular Expressions (RegEx) for extracting the notes from the text .ahk file.
  3. Formatting problems arose when using variables in the MsgBox command.

I discuss the file location problem in this blog while saving the other two issues for future blogs.

Finding the AHK File for Extracting Notes

In most cases after extracting target file data from the Windows shortcut, the operating file will consist of either an .ahk script or .exe program. In the case of the .ahk file, AutoHotkey can directly access the text file notes, but .exe files do not respond to the text parsing. The solution to the .exe problem consists of either finding a corresponding uncompiled .ahk file or adding a new .ahk notes file using the same name and location as the .exe file.

If the target filename contains “.exe”, then the script changes the extension to “.ahk” before checking for its existence and attempting to load it into a variable:

If InStr(Location,".exe")
    Location := StrReplace(Location, ".exe" , ".ahk")

If the .ahk file does not exist, then AutoHotkey exits the subroutine using the Return command and displays “No notes found!”:

If !FileExist(Location)
{
    MsgBox No notes found!
    Return
}

The Startup folder may contain any number of shortcuts targeting non-.ahk files. For example, the main menu item Dropbox.lnk (shown in the image toward the beginning of this blog) will never own a corresponding .ahk file—unless I create one. (I could add a dummy .ahk file just to provide some description, but for now, I don’t need one.)

Note: If creating special non-functioning .ahk files, enclose the notes between the /* and */ comment markers. In fact, you must place any .ahk notes between the first set of these two comment markers in all files accessed.

Since AutoHotkey opts out of this subroutine when the .ahk file does not exist, the script now reads the contents of any detected .ahk file into a variable:

FileRead, FileVar, %Location%

Now set for parsing using the RegExMatch() function, the following line does its work:

Found := RegExMatch(FileVar, "s)/\*(.*?)\*/", NotesVar)
Regular Expressions in AutoHotkey
Regular Expressions (RegEx) can be mysterious in any language.

Rather than undertake a lengthy discussion about Regular Expressions right now, I’ll save the discussion for my next blog. I’ll have more time to expound on the virtues of RegEx. (In a hurry to learn more about Regular Expressions? See the AutoHotkey RegEx Quick Reference page or my book A Beginner’s Guide to Using Regular Expressions in AutoHotkey.)

Tip: Not all of your AutoHotkey apps use or need auto-startup with Windows. However, you can modify the script to work with any folder. Then add shortcuts for those apps you want readily available, even though, you don’t want them to load when booting your computer.

* * *

This happens! I start off writing what I consider a simple subroutine, yet it incorporates a number of different aspects of AutoHotkey. While many people may understand what the script does by merely looking at the code, I can’t assume this for everyone. For new or inexperienced readers, this may be brand new material. I’ll spend more time pondering both the delights of Regular Expressions (RegEx) and the problems with MsgBox text formatting in future blogs.

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“!

2 thoughts on “Peeking at Notes Inside Auto-Startup AHK Script Files (AutoHotkey Startup Control)

Leave a Reply to Regular Expressions (RegEx) for Mining Text in Files (AutoHotkey Startup Control) | Jack's AutoHotkey Blog Cancel 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