Use Alternative Filename Extensions for Special Format Files (Part Five: Finishing AutoHotkey GUI Scripts)

While Changing the Saved Filename Extension in the InstantHotstring.ahk Script Helps Protect Original AutoHotkey Files, the Technique Offers Additional Benefits…Plus, a No-Wait Progress Bar for Instant-Saves

Over the course of the past few blogs, I added protection to files containing AutoHotkey code by both including a one-line file header and changing the saved filename extension to .hsf. These steps have resolved my concern about overwriting any AutoHotkey scripts—from which I may have extracted Hotstrings and loaded them into the “under-construction” InstantHotstringMenuBar.ahk app. At times, I thought that adding the two techniques might be overkill but now I’ve come to realize that using an alternative extension provides benefits that may prove even more useful than my initial attempt at protecting .ahk files.

While I rarely change the extension of a file, I recognize times when it can offer extra flexibility. For example, during my review of the pending Version 2.0 of AutoHotkey, I added the extension .ahk2 to any script written in V2.0 code as discussed in “How to Fool Around with the New AutoHotkey Version 2.0.” By making AutoHotkey V2.0 the default for opening any .ahk2 file, I could run both V1.1 and V2.0 scripts without making changes in my setup each time I ran a different version script.

The Downside to Invented File Extensions

As a rule, I wouldn’t recommend that someone invent their own filename extension. While the InstantHotstring.ahk script reads and extracts Hotstrings from any text file, saving the Hotstrings the latest version of InstantHotstringMenuBar.ahk with the .hsf extension eliminates the standard Windows AutoHotkey script auto-load feature available for .ahk files (i.e. double-click on the filename in Windows File Explorer). You can continue to add the new .hsf files to any script using the #Include directive but direct double-click loading requires the addition of the .hsf extension to the file defaults in your Windows setup. (You may soon discover a reason not to want to auto-load .hsf files in AutoHotkey—as discussed below.)

Plus, any extension you pick for a new file type may interfere with another common file extension. A Google search for .hsf (Hotstring file) turns up:

“What is an HSF file? File created in the HOOPS Stream Format (HSF), a CAD publishing format developed by Tech Soft 3D for visualization; supported via plug-in or as a native publishing format by several CAD applications, including SolidWorks and CATIA v5.”

Google Search

Since I don’t use any CAD applications, the extension does not cause a problem for me. However, if you do, using .hsf as an extension could gum up the works. Fortunately, changing the saved-file extension in the InstantHotstring.ahk script only requires a simple search and replace.

New File Extension Benefits

When using an AutoHotkey script to write special format files, you’ll note a number of reasons to use a different extension. Some extensions such as .ini and .csv offer more traditional uses but when confronted with writing an altered format, you may decide upon a new extension to indicate the difference. The benefits may include:

  1. Protect Other File Types
    By changing special format file extensions, any instant-save feature only updates that type of file—leaving other file types (i.e. .ahk) untouched. This rational prompted my original set up of the new .hsf extension.
  2. Distinguish Special Purpose Files
    By adding a new extension, the user immediately discerns the new file type in Windows File Explorer—plus, the FileSelectFile command can filter out other types of files.
  3. Set Up Default Double-click Loading
    With the proper Windows setup as a default, the user can auto-load special file types into a targeted app. This may turn out to be the most important benefit of special file extensions.

File Overwrite Protection

It wasn’t until I started implementing an instant-save (CTRL+S) feature in the InstantHotstringMenuBar.ahk script that I began considering the creation of a new file extension for my InstantHotstring generated files. By saving the Hotstrings in a .hsf file, all of the .ahk files remain untouched—even though the app may have originally extracted all of its Hotstrings from an AutoHotkey script file (.ahk).

By adding two lines of code, the script now always concatenates the .hsf extension before saving the file:

If !InStr(SelectFile,".hsf")
  SelectFile .= ".hsf"

If the selected file name does not contain “.hsf”, the script uses the “.=” operator to append the extension to the end of the filename. The latest modification of the script does not allow the overwriting of any file type except .hsf—including during the instant-save routine (CTRL+S).

Distinguishing Special Purpose Files

When viewing filenames in Windows File Explorer, a user can immediately identify and sort by the unique file extension. Knowing the file extension helps in understanding the purpose and content of the file.

When opening or saving files, the FileSelectFile command allows the filtering of file types by extension. This makes it easier to isolate file types in long directories.

For saving files:

FileSelectFile, SaveFile , S, %FilePath%\%SaveFile%, 
              , Hotstrings(*.ahk;*.hsf)

For opening files:

FileSelectFile, OpenFile , , %FilePathr%\Hotstrings\, 
              , Hotstrings(*.ahk;*.hsf)

(The above snippets use line continuation techniques to wrap lines of code for display purposes.)

Both of these examples demonstrate how to filter file types by extension.

Tip: While the AutoHotkey FileSelectFile command does allow multiple file extensions in a semi-colon delimited list—as shown above, the filter dropdown list does not allow multiple options—other than the one additional default All Files (*.*). However, you can further filter files by manually entering wildcards. For example, enter “*.hsf” into the File Name field and press Return. Only filenames containing “.hsf” appear in the viewing window.

Future Use of New File Extension for Double-Click Hotstring Auto-Loading

While I didn’t initially consider this possibility, using a special file extension will allow me to set up Windows to load that file into an instance of the InstantHotstring app with a double-click of the filename in Windows File Explorer (not yet implemented). Most Windows programs offer this type of auto-load. Now I only need to review the requirements for the AutoHotkey script and decide whether I plan to auto-load into a new instance of the app or the same open window.

I’ve scheduled this exploration for a future blog after I start looking at the Open File menu item. At that time, I’ll explain the steps required for adding auto-load to an AutoHotkey script…if I can figure it out.

Progress Bar for Instant-Save

Since I want the instant-save operation to run to the end without pausing for a MsgBox window, I’ve switched to a Progress command.

Similar to the command used when loading Hotstrings, the new Progress Bar increments while saving Hotstrings to the current target file:

Progress, R0-%TotalHotstringCount% x640 y540
        , Saving %TotalHotstringCount% Hotstrings!

The variable TotalHotstringCount keeps track of the number of new Hotstrings whenever adding them to the app via the Load Hotstrings button routine.

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 )

Facebook photo

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

Connecting to %s