Sensing AutoHotkey Editing Changes for Instant Save (Part Three: Finishing AutoHotkey GUI Scripts)

By Detecting Modifications in Edit Data We Know When to Activate Instant-Save Routines

Any change too active Hotstrings appends an asterisk (*) to the current open filename and enables the Save option.

In the last blog (“GUI Menu Bar “Save” Item Complications (Part Two: Finishing AutoHotkey GUI Scripts)“), I discussed the need to add a special header to a unique type of data file—InstantHotstring generated Hotstrings. This header helps to differentiate between AutoHotkey .ahk files—which you may not want to overwrite—and files generated by the InstantHotstring.ahk script.

When adding a Save option (instant-save using the CTRL+S key combination) to the menu bar, many Windows apps concatenate an asterisk (*) to the file name in the title bar—alerting the user to changes. In this blog, I add a similar change-detecting feature which both displays the appended asterisk and enables an instant-save routine.

Note: I’ve added a new extension for InstantHotstring generated files, .hsf (Hotstring File). This provides added protection for .ahk files while making instant-save operations safer. You can activate the .hsf files in AutoHotkey by adding them to your script using the #Include directive or manually change the extension to .ahk for direct loading.

Setting the Window Title Bar to the Filename

Before detecting changed data, the script designates the appropriate Save target file by setting the window title bar to that filename. Displaying the filename in the title bar tells the user the current target filename thereby bypassing the FileSelectFile window each time it performs the Save operation. If reading Hotstrings into the InstantHotstring app, the Load Hotstrings operation must check for the proper type of file, save the filename, then display that name in the GUI window title bar using the WinSetTitle command.

The following code checks the file type by reading the first line of the file, parses the filename using the SplitPath command, saves the filename to the variable WindowTitle, then executes the WinSetTitle command:

  FileReadLine, FileHeader, %OpenFile%, 1
  If InStr(FileHeader,"InstantHotkey generated file")
    SplitPath, Openfile, FileName
    WindowTitle := FileName
    WinSetTitle, %WindowTitle%

This check occurs each time the script loads a new file—unless merely appending additional Hotstrings from another file. A similar check sets the title bar filename whenever the Save As… routine writes a new file.

Detecting Data Changes in the Set Hotstring Routine

When working in a GUI Edit field, “a g-label such as gMySubroutine may be listed in the control’s options. This would cause the MySubroutine label to be launched automatically whenever the user or the script changes the contents of the control.” This makes detecting those actions easy—although the script still needs a subroutine for altering the filename in the title bar.

With no such gLabel option in the InstantHotstring.ahk script, The Set Hotstring button which initiates any updates or new active Hotstrings must report data changes. I added the following code to note whenever the user clicks the “Set Hotstring” button when a filename resides in the title bar:

If (!InStr(WindowTitle,"*") and WindowTitle)
  WindowTitle .= "*"
  WinSetTitle, %WindowTitle%
  Menu, FileMenu, Enable, &Save`tCtrl+S

This routine only runs If the WindowTitle variable does not contain the asterisk and the variable WindowTitle exists—meaning an appropriate filename appears in the title bar. Since the snippet both adds the asterisk (*) and enables the Save menu item (and key combination), the script won’t overwrite a non-InstantHotstring file via the instant-save (Save) menu item.

Removing the Asterisk and Disabling the Save Menu Item after Saving a File

The following snippet now appears in the SaveHotstrings subroutine called by the “Save Hotstrings” button and the new Save As… menu item subroutine:

If (A_ThisMenuItemPos != 5 and !FileExist(SaveFile))
    SplitPath, SaveFile, CurrentFile
    WindowTitle := CurrentFile
    WinSetTitle, %WindowTitle%
    Menu, FileMenu, Disable, &Save`tCtrl+S

This code removes the asterisk from the title bar and disables the Save menu item. By placing the snippet in the SaveHotstrings subroutine, it runs every time the user either clicks the instant-save menu item (Save CTRL+S) or saves the Hotstrings to a new filename. The If conditional prevents the changing of the Save filename when merely appending Hotstrings to an alternative file—either via the Append Hotstring to File menu option or when selecting Append during the Save As… option.

I used a little bit of sleight-of-hand to make the same snippet work for both the instant-save Save menu item and the Save As… menu item. I’m not sure that everyone would approve of how I incorporated the disparaged GoTo command, but it makes quick use of previously written code without a lot of mental gymnastics—a topic for next time.

Note: While I view this script as work-in-progress, I provide the current version of the new code for the curious. You can find the most recent iteration of the InstantHotstringMenuBar.ahk script on the “ComputorEdge Free AutoHotkey Scripts” page—updated on July 5, 2020. Although only partially operational, you can find the discussed changes in context—and a few more additions subject to future inspection. While these blogs demonstrate the techniques I plan to implement, the script requires a great deal more work before it’s ready for general use.

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