Skip to content

Context Menu Binding

ContextMenuBinding dataclass

ContextMenuBinding(
    explorer_item: str | ExplorerItemType,
    access_scope: MenuAccessScope = ALL_USERS,
)

Represents a binding for a context menu.

Bindings determine the basic conditions for a context menu to appear when right-clicking items.

For example binding a context menu to files/folders/drives mean that it displays when right-clicking them.

Tip

For more advanced control of conditions see Condition feature, ICondition, and conditions.

Attributes:

Name Type Description
menu_item_type

Determines which object type the context menu is relevant for. For example files/folders/drives/etc. Should be a string. See ExplorerItemType for options and descriptions.

access_scope MenuAccessScope

Bind to current user or all users.

to_path

to_path() -> RegistryPath

Compose the registry path that match the binding.

Example
ContextMenuBinding(
    MenuAccessScope.ALL_USERS,
    ExplorerItemType.ALL_FILES,
).construct_registry_path()
# HKEY_LOCAL_MACHINE\Software\Classes\*\shell
ContextMenuBinding(
    MenuAccessScope.ALL_USERS,
    ExplorerItemType.DESKTOP_BACKGROUND,
).construct_registry_path()
# HKEY_LOCAL_MACHINE\Software\Classes\DesktopBackground\shell
ContextMenuBinding(
    MenuAccessScope.ALL_USERS,
    ExplorerItemType.SPECIFIC_FILE_TYPE.format(file_type="image"),
).construct_registry_path()
# HKEY_LOCAL_MACHINE\Software\Classes\DesktopBackground\shell
Source code in context_menu_toolkit/context_menu_bindings.py
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
64
65
def to_path(self) -> RegistryPath:
    r"""Compose the registry path that match the binding.

    Example:
        ```python
        ContextMenuBinding(
            MenuAccessScope.ALL_USERS,
            ExplorerItemType.ALL_FILES,
        ).construct_registry_path()
        # HKEY_LOCAL_MACHINE\Software\Classes\*\shell
        ```

        ```python
        ContextMenuBinding(
            MenuAccessScope.ALL_USERS,
            ExplorerItemType.DESKTOP_BACKGROUND,
        ).construct_registry_path()
        # HKEY_LOCAL_MACHINE\Software\Classes\DesktopBackground\shell
        ```

        ```python
        ContextMenuBinding(
            MenuAccessScope.ALL_USERS,
            ExplorerItemType.SPECIFIC_FILE_TYPE.format(file_type="image"),
        ).construct_registry_path()
        # HKEY_LOCAL_MACHINE\Software\Classes\DesktopBackground\shell
        ```
    """
    self._validate_parameters_provided()

    return RegistryPath(self.access_scope) / self.explorer_item / "shell"