Skip to content

Manage context menus by automating registry writes using ahk.

Notifications You must be signed in to change notification settings

SheriffBuzz/contextmenu

Repository files navigation

Contextmenu

Manage context menus by automating registry writes using ahk. Additional visual guides for FileTypesMan.

Resources

How to open Registry Editor

FileTypesMan Scroll to the bottom of the page for the download

FileTypes in Windows

Best Practices when working with the Registry

  • Backup your Windows Registry

  • If you download registry (.reg) scripts from the internet, open them in a text editor and inspect the keys they are modifying, before running them. This script modifies registry keys that have the following patterns:

    • Computer\HKEY_CLASSES_ROOT*\shell - Context menu when right clicking a file of any extension
    • Computer\HKEY_CLASSES_ROOT\.{fileExtension}\shell - Context menu when right clicking a file of a specific extension
    • Computer\HKEY_CLASSES_ROOT\Directory\shell - Context menu when right clicking a folder
    • Computer\HKEY_CLASSES_ROOT\Directory\Background\shell - Context menu when right clicking the empty space in a folder

Goals

  • Automate registry writes via script instead of using RegEdit or 3rd Party programs
    • Don't need to use GUI every time on each device when working with multiple devices
  • Allow storing configuration details for ahk scripts and icon files in non-absolute paths

Project setup

Find below the project setup and structure.

  • Clone the project to any location.
  • Open settings.ini
    • Expand environment variables - 0 or 1. Determines what is stored in the "command" registry key. If you specify a program path %SYSTEMDRIVE%\Program Files\SomeProgram\SomeExecutable.exe with the flag on, it will be expanded to C:\Program Files\SomeProgram\SomeExecutable.exe where "C:" is the value of SYSTEMDRIVE.
  • \resources - stores context menu options as csv
  • \bin - location for compiled ahk scripts and other executables. Can refer to a file name + extension in our csv config if programs are stored here, otherwise they must be referred to by an absolute path or one with environment variables

Add context menu entry for a specific file type

  • Menu entry will appear at the top of the context menu, above any entries that are defined for all file extensions

  • Certain file extensions get associated with an application, often called user choice. The way it is set up in Windows 8+ makes it harder to edit the registry directly, as described here

    Microsoft decided in Windows 8 (probably for security reasons) that users should be able to set default programs only via the built in GUI. I.e. by design, you are not supposed to be able to set default handlers in a script or programmatically.

    The Hash value is used to prove that the UserChoice ProgId value was set by the user, and not by any other means. This works as long as Microsoft keeps the algorithm which generates the Hash, and the mechanism for verifying the ProgId using the Hash, a secret.

    In theory you could figure out the secret to set the Hash (and possibly other hidden OS settings), but you would have no guarantee of it's reliability; the next Windows Update might break your method, for example. You probably just need to adapt to the change, and live with using the new methods Microsoft built in to the OS.

The easy way around this is to use FileTypesMan

  1. Navigate to the extension and click on it, so it is selected in the top view
  2. Right Click the bottom menu -> New Action
  3. Give a name, and command as "C:\Program Files\HandBrake\HandBrake.exe" "%1".
    • "%1" specifies that the file/folder path selected in Windows Explorer will be passed to the application.
    • Alternatively, select the program from the list of running programs.
  4. Optionally select the path of an .ico or .exe to set the icon
2022-07-16.19-09-23.mp4

Multiple context menu entries of the same file extension

If you already have menu entries for the same extension, the one you add might not be at the top. The order is determined by the name of the registry keys. For FileTypesMan, this field is read only, but you can change it with RegEdit.

  • If your file extension is not associated with a User Choice, it will lead you to Computer\HKEY_CLASSES_ROOT.{extension}\shell instead of Computer\HKEY_CLASSES_ROOT\AppX6eg8h5sxqq90pv53845wmnbewywdqq5h\Shell\ but the process is the same.

image

  1. Click on the key on the explorer view (left side of registry editor). For our demo it will be "Open with Handbrake"
    • Make sure the key that is selected is the name of the action, not "shell" or "command"
  2. Double click the "Default" key in the right hand side. Set the value to what you want to be displayed on the context menu.
  3. On the left side in the explorer view, right click on the key you want to be at the top, and prepend it with a number or special character so it comes first alphabetically.
    • Make sure the key that is selected is the name of the action, not "shell" or "command"
2022-07-16.19-25-26.mp4

Now the menu option will be on top.

image

Detach file extension from User Choice

To avoid changing the icon or adding context menu items for all file extensions associated with a User Choice, remove User Choice using FileTypesMan. Optionally change the FileType if multiple extensions share the same file type.

  1. Open the ext in FileTypesMan and click the ... for User Choice. Click Detach File Type.

    • You may get an error, but it should still work.
  2. Open RegEdit and navigate to Computer\HKEY_CLASSES_ROOT\.{extension}

  3. For the default key, rename it to something else, commonly {extension}file or .{extension}_auto_file

  4. Create a new key under Computer\HKEY_CLASSES_ROOT with the same name

  5. Run WriteContextMenu.ahk, or follow the steps for Add Context menu entry for a specific file type again.

    • The script will write context menu entries or icons to the new handler. Other file extensions that use the old handler will be unaffected.
    • Only takes effect after system restart or calling SHChangeNotify. You can also open a file extension in FileTypesMan and click ok on the popup without changing anything, which calls this function.
2022-07-17.11-40-00.mp4
2022-07-17.11-46-28.mp4

About

Manage context menus by automating registry writes using ahk.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages