Skip to content

Registry Exporter

RegistryExporter

Knows how to create a Registrykey from menu.

export_reg_file

export_reg_file(
    menu: ContextMenu, bindings: list[ContextMenuBinding]
) -> list[str]

Export the Context Menu as a .reg file format.

Example
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\Software\Classes\*\shell\ConvertVideo]
"MUIVerb"="Convert mp4..."

Returns:

Type Description
list[str]

A list of lines of the .reg file.

References
Source code in windows_context_menus/registry/exporting/registry_exporter.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def export_reg_file(self, menu: ContextMenu, bindings: list[ContextMenuBinding]) -> list[str]:
    r"""Export the Context Menu as a .reg file format.

    Example:
        ```
        Windows Registry Editor Version 5.00

        [HKEY_LOCAL_MACHINE\Software\Classes\*\shell\ConvertVideo]
        "MUIVerb"="Convert mp4..."
        ```

    Returns:
        A list of lines of the .reg file.

    References:
        [^1]: <https://support.microsoft.com/en-us/topic/how-to-add-modify-or-delete-registry-subkeys-and-values-by-using-a-reg-file-9c7f37cf-a5e9-e1cd-c4fa-2a26218a1a23>
    """
    built_menu: RegistryKey = self.export_tree(menu)

    lines = [
        "Windows Registry Editor Version 5.00",
        "",
        "; Created by: windows_context_menus @ https://github.com/orishamir/WindowsContextMenus/",
        f"; Created on: {datetime.now().strftime('%B %d, %Y')}",
        "",
    ]

    for i, binding in enumerate(bindings):
        lines.append(f";;; menu for binding #{i + 1}: access_scope={binding.access_scope}, item_type={binding.explorer_item}")
        lines.extend(
            built_menu.export_reg(
                binding.to_path(),
            ),
        )
        lines.append(f";;; end of menu for binding #{i + 1}: access_scope={binding.access_scope}, item_type={binding.explorer_item}")

    return lines

export_tree

export_tree(menu: ContextMenu) -> RegistryKey

Export as a RegistryKey tree.

Source code in windows_context_menus/registry/exporting/registry_exporter.py
65
66
67
68
69
70
71
72
73
74
def export_tree(self, menu: ContextMenu) -> RegistryKey:
    """Export as a RegistryKey tree."""
    tree = RegistryKey(name=menu.display_text)

    self._export_attributes(menu, tree)

    if menu.submenus:
        self._export_submenus(menu, tree)

    return tree