Saving Default Data in the Windows Registry (Part Six: Finishing AutoHotkey GUI Scripts)

Many AutoHotkey Users Save Settings in an INI File…However, the Windows Registry May Offer More Security and Stability

We tend to feel nervous about working with the Windows Registry. After all, a wrong step could conceivably cause harm to the operating system setup. Yet when taking the appropriate steps, this built-in Windows database can offer us a number of benefits:

  1. The Windows Registry is always there.
  2. It’s not easy to accidentally delete a Windows Registry entry.
  3. The average computer user won’t know where to find these special data entries.

Caution: When writing or editing the Windows Registry ensure that you use new, unique entry names and do not duplicate previously existing data items. Use Windows the RegEdit utility—Run (+R) RegEdit—to check for possible conflicting Registry entries.

In the InstantHotstringMenuBar.ahk script, I separated the setting of the default loading file from the SaveHotstrings subroutine (discussed in previous blogs) into a separate Label activated through the Set default file menu item or the Ctrl+D shortcut key combination. Plus, I added a new option to remove the default file setting:

SetDefaultFile:

RegRead, OpenFile, HKCU\Software\InstantHotstring, DefaultFile
If (OpenFile = 0 or !OpenFile)
    DefaultFileName := "No current default file"
Else
    DefaultFileName := "Current default file" . OpenFile
MsgBox, 3, , %DefaultFileName%
	!`rChange default file
        ?`rYes       — Change current file on Startup
        !`rNo        — Remove setting (no default)
	!`rCancel  — Keep current default file!
  IfMsgBox, No
  {
    RegWrite, REG_SZ, HKCU\Software\InstantHotstring, DefaultFile, 0
    Return
  }
  IfMsgBox, Cancel
    Return
  IfMsgBox, Yes
    Return
If WindowTitle != ""
	SaveFile := WindowTitle
If (OpenFile = 0 or !OpenFile)
  FileSelectFile, SaveFile , S, %A_WorkingDir%\Hotstrings, , Hotstrings(*.ahk;*.hsf)
Else
  FileSelectFile, SaveFile , S, %FilePath%\%SaveFile%, , Hotstrings(*.ahk;*.hsf)
If ErrorLevel     ; "Cancel" button, close, or Escape
   Return
RegWrite, REG_SZ, HKCU\Software\InstantHotstring, DefaultFile, %SaveFile%

Return

This subroutine allows the user to set a new default, remove the filename, or maintain the current default (if any) in the Registry.

First, the SetDefaultFile subroutine uses the RegRead command to read the current value of the setting in Windows Registry:

RegRead, OpenFile, HKCU\Software\InstantHotstring, DefaultFile

The MsgBox displays the current value of the default file and drives the file selection decisions (replace, remove, or leave in place).

Finally, the subroutine uses the RegWrite command to save changes (if any):

RegWrite, REG_SZ, HKCU\Software\InstantHotstring, DefaultFile, %SaveFile%

For more information on how to work with the Windows Registry, see “Save AutoHotkey Script Settings in Your Windows Registry” and “How to Use the Windows Registry to Save Data Table Records.”

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

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