Save AutoHotkey Script Settings in Your Windows Registry

While You Can Use an External File to Save Your Script Defaults, You Can Also Call-on the Windows Registry to Store App Settings

In most cases, I’ve used a separate file (text or INI) to save settings. While this works well, it requires the creation and tracking of that independent file. For the script to read the settings, it must know where to locate that file. But, if you want to save script settings without the baggage of that extra file, then consider using your Windows Registry.

You’ll find a few advantages to maintaining your script settings in the Windows Registry:

  1. Unlike a separate settings file, you don’t need to keep track of the Windows Registry’s location—it never moves. Regardless of where you locate your AutoHotkey script on your computer, you will always find its settings in the exact same place.
  2. You won’t accidentally lose your settings by manually deleting what may seem like an extra file in Windows File Explorer.
  3. Your settings remain semi-hidden from public view in a semi-permanent form.

This approach to storing your script defaults deep inside the recesses of Windows adds an air of mystery to your apps—especially if you compile them into EXE files.

The Ambiguous American/British Numeric Date Format Problem

Format PickIn the most recent version of the DateStampConvert.ahk script, whenever AutoHotkey encounters an ambiguous numeric date format (with both month and day number 12 or below, e.g. 10-1-2018—American or British date?), I used a pop-up MsgBox to force a decision for the appropriate conversion. But most people—depending upon where they live and work in the world—may prefer to automatically default to a specific format rather than making the choice every time they run into the question. By saving that setting (American or British) in the Windows Registry, we eliminate the redundant decision points—only resetting that default when our predominant date sources change.

The Windows Registry

In past years, I’ve recommended that Windows users avoid tromping around inside the Windows Registry via RegEdit.exe—the built-in Windows Registry Editor. Make a wrong move and you can cause serious problems for your Windows system or specific applications. But when you use AutoHotkey registry commands, you enter this internal operating system database with a surgical knife. You don’t run the same risks you might encounter when you explore the registry with RegEdit.

If you open RegEdit (microsoft_key+r, enter regedit in the Open: box and select OK), you’ll find thousands of entries involving a multitude of Windows settings—most of which give no hint as to their purpose. Making changes here can be dangerous. When we use the AutoHotkey registry commands, we only create and edit the registry entries we need— not touching the massive number of other settings.

Windows Registry Settings

The Windows Registry uses a hierarchical type of structure. First broken into major categories (hardware and software) called Root Keys:

  • HKEY_LOCAL_MACHINE or HKLM
  • HKEY_CURRENT_CONFIG or HKCC
  • HKEY_CLASSES_ROOT or HKCR
  • HKEY_CURRENT_USER or HKCU
  • HKEY_USERS or HKU

Each section serves specific functions for your Windows computer. For our purpose, we access the root key HKEY_CURRENT_USER (HKCU) which contains settings for the current logged on user. We save our DateStampConvert.ahk default here.

RegEdit
Using the AutoHotkey RegWrite command, we create the Format key at the HKCU\Software\DateStampConvert registry location.

Whenever loading the script, AutoHotkey reads the Windows Registry key to load the default ambiguous date format. We then use the right-click context menu for the apps System Tray icon to change that default in the registry.

Reading a Windows Registry Key with AutoHotkey

When working with the Windows Registry in AutoHotkey, we use the commands RegRead, RegWriteRegDelete, and  Loop (registry). The RegRead command takes the form [v1.1.21+]:

RegRead, OutputVar, KeyName, ValueName

Although now deprecated, the following RegRead format also works in AutoHotkey V1.1 and earlier:

RegRead, OutputVar, RootKey, SubKey, ValueName

The current form of the RegRead command merely removes the comma and RootKey replacing the two keys with a single key path. For example:

RegRead, Format, HKCU, Software\DateStampConvert, Format

becomes:

RegRead, Format, HKCU\Software\DateStampConvert, Format

AutoHotkey Version 2.0 Note: In AutoHotkey V2.0, the RegRead command turns into a function:

Value := RegRead(KeyName, ValueName)

Detecting and Creating a Windows Registry Key

Attempting to read a Windows Registry key offers the best method for detecting its existence. If you use the RegRead command to access a non-existent key, AutoHotkey sets the ErrorLevel variable to 1. We use this error to initiate the creation of the key when we run the script for the first time:

DateStampConvertAutoExec:

Global Format     ; For use in functions.
RegRead, Format, HKCU, Software\DateStampConvert, Format 
If ErrorLevel = 1
{
  RegWrite, REG_SZ, HKCU\Software\DateStampConvert, Format, American
  Format := "American"
}
Menu, Tray, Add , % "Default Format " Format " (Change)", ChangeDefault

Return

During the lead-off load, we set up the Windows Registry key (Format) for saving the numeric date default value. The RegRead command attempts to grab the value of the Format key found at the registry location HKCU\Software\DateStampConvert and store it to the variable Format. If not found (ErrorLevel = 1), AutoHotkey creates it using the RegWrite command with the “American” default while setting the Format variable to the same value.

DateStampMenu

In the snippet above, the Global Format statement makes the variable Format available for use anywhere in the script—including inside functions.

The last Menu command statement adds an item to the Systems Tray right-click context menu displaying the current default format while offering the one-click option to change the setting using the Label subroutine ChangeDefault (shown at right).

Windows Registry Key Value Types

In most cases, one of two key types offers a workable solution for the value in the RegWrite command: REG_SZ or REG_DWORD. Although a little counterintuitive, use REG_SZ for text or character strings and REG_DWORD for integers. The value type REG_SZ offers the most flexibility since it may include almost any character. (For multiple lines of text, use REG_MULTI_SZ.)

Toggling the Default Ambiguous Date Format

The script uses its System Notification Tray icon’s right-click context menu to both display and change the current ambiguous format default value. I could have used menu check marks to indicate defaults on and off, but merely renaming the menu item using the Menu, Tray, Rename command provided an easier (and clearer) solution. The ChangeDefault toggling subroutine appears as follows:

ChangeDefault:
If (Format = "American")
{
  RegWrite, REG_SZ, HKCU\Software\DateStampConvert, Format, British
  Format := "British"
  Menu, Tray, Rename, Default Format American (Change)
          , Default Format British (Change)
}
Else
{
  RegWrite, REG_SZ, HKCU\Software\DateStampConvert, Format, American
  Format := "American"
  Menu, Tray, Rename, Default Format British (Change)
          , Default Format American (Change)
}
Return

Note that the toggling routine only changes the Windows Registry entry and renames the menu item while updating the Format variable. The script does not read the registry key again until you reload the script—then using the current value of the key.

The Default Ambiguous Date Decision

Rather than popping up the decision MsgBox for the ambiguous numeric date portion of the script, add the following to the DateStampConvert.ahk routine while removing the MsgBox:

If (Format = "American")
  Return MakeStamp(Date3,Date1,Date2) ; 10-1-18
Else
  Return MakeStamp(Date3,Date2,Date1) ; 1-10-18
}

This eliminates the need to choose each time you encounter the numeric date format ambiguity.

Multiple Uses of the Windows Registry with AutoHotkey

I’m considering rewriting the ToDoList.ahk script to use the Windows Registry rather than a separate text file for the current task list. This would eliminate the need to keep track of the ToDoList.txt file. Plus, my script has occasionally wiped out the entire list. (Don’t yet know why.) I think that using the Windows Registry may create a more robust solution. When I finish it, I’ll preview it in this blog. Click the Follow button at the top right of this page for e-mail notifications of new blogs.

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

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