windowsctxmenu 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- windowsctxmenu-1.0.0/.gitignore +19 -0
- windowsctxmenu-1.0.0/LICENSE +21 -0
- windowsctxmenu-1.0.0/PKG-INFO +99 -0
- windowsctxmenu-1.0.0/README.md +63 -0
- windowsctxmenu-1.0.0/examples/add.py +23 -0
- windowsctxmenu-1.0.0/examples/remove.py +52 -0
- windowsctxmenu-1.0.0/pyproject.toml +29 -0
- windowsctxmenu-1.0.0/src/windowsctxmenu/__init__.py +14 -0
- windowsctxmenu-1.0.0/src/windowsctxmenu/addmenu.py +79 -0
- windowsctxmenu-1.0.0/src/windowsctxmenu/cli.py +49 -0
- windowsctxmenu-1.0.0/src/windowsctxmenu/contextmenu.py +147 -0
- windowsctxmenu-1.0.0/src/windowsctxmenu/utils.py +132 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Beltrán Offerrall
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: windowsctxmenu
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Add, remove and block Windows right-click context menu entries from Python.
|
|
5
|
+
Project-URL: Homepage, https://github.com/offerrall/windowsctxmenu
|
|
6
|
+
Project-URL: Repository, https://github.com/offerrall/windowsctxmenu
|
|
7
|
+
Author: windowsctxmenu authors
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 Beltrán Offerrall
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Keywords: context-menu,registry,shell,windows,winreg
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
33
|
+
Classifier: Programming Language :: Python :: 3
|
|
34
|
+
Requires-Python: >=3.10
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# windowsctxmenu
|
|
38
|
+
|
|
39
|
+
Get your Windows right-click menu exactly the way you want it, from Python.
|
|
40
|
+
|
|
41
|
+
The idea: you write **one script** with your setup (what to remove, what to add,
|
|
42
|
+
what to block) and carry it to every new PC or every reinstall. Run it and your
|
|
43
|
+
context menu is identical everywhere — no manual registry editing.
|
|
44
|
+
|
|
45
|
+
## Requirements
|
|
46
|
+
|
|
47
|
+
- Windows 11
|
|
48
|
+
- Python >= 3.10
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install windowsctxmenu
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## API
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from windowsctxmenu import (
|
|
60
|
+
add_shell, add_handler, unblock,
|
|
61
|
+
remove_shell, remove_handler, remove_shellnew, block,
|
|
62
|
+
)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
| Function | What it does |
|
|
66
|
+
| --- | --- |
|
|
67
|
+
| `add_shell(name, command, ...)` | Add a classic verb under `...\shell` |
|
|
68
|
+
| `add_handler(name, clsid, ...)` | Add a COM handler |
|
|
69
|
+
| `unblock(name_or_clsid)` | Unblock a blocked CLSID |
|
|
70
|
+
| `remove_shell(name, ...)` | Remove a classic verb |
|
|
71
|
+
| `remove_handler(name, ...)` | Remove a COM handler |
|
|
72
|
+
| `remove_shellnew(key_path)` | Remove an entry from the "New" submenu |
|
|
73
|
+
| `block(name_or_clsid)` | Block a built-in element by CLSID |
|
|
74
|
+
|
|
75
|
+
`scope`: `file`, `directory`, `background`, `folder`, `drive`, `allfiles`,
|
|
76
|
+
`all`, an extension (`.zip`), a ProgID glob (`VLC.*`), or `sfa:<glob>`.
|
|
77
|
+
|
|
78
|
+
Writing to `HKLM` (shared across users) requires running **as administrator**;
|
|
79
|
+
otherwise the action is reported as no-permission and continues.
|
|
80
|
+
|
|
81
|
+
## Examples
|
|
82
|
+
|
|
83
|
+
Everything is in the [`examples/`](examples) folder:
|
|
84
|
+
|
|
85
|
+
- [`examples/add.py`](examples/add.py) — add entries (open folder with VS Code,
|
|
86
|
+
PowerShell here, ...).
|
|
87
|
+
- [`examples/remove.py`](examples/remove.py) — full menu cleanup.
|
|
88
|
+
|
|
89
|
+
Copy one, tweak it to taste, and that's your per-PC script.
|
|
90
|
+
|
|
91
|
+
## `switchmenu` command
|
|
92
|
+
|
|
93
|
+
Toggle the right-click menu between the **classic** (Windows 10) and the **new**
|
|
94
|
+
(Windows 11) one. Restarts Explorer to apply instantly. No administrator needed:
|
|
95
|
+
everything lives under `HKEY_CURRENT_USER`.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
switchmenu
|
|
99
|
+
```
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# windowsctxmenu
|
|
2
|
+
|
|
3
|
+
Get your Windows right-click menu exactly the way you want it, from Python.
|
|
4
|
+
|
|
5
|
+
The idea: you write **one script** with your setup (what to remove, what to add,
|
|
6
|
+
what to block) and carry it to every new PC or every reinstall. Run it and your
|
|
7
|
+
context menu is identical everywhere — no manual registry editing.
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
- Windows 11
|
|
12
|
+
- Python >= 3.10
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install windowsctxmenu
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## API
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from windowsctxmenu import (
|
|
24
|
+
add_shell, add_handler, unblock,
|
|
25
|
+
remove_shell, remove_handler, remove_shellnew, block,
|
|
26
|
+
)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
| Function | What it does |
|
|
30
|
+
| --- | --- |
|
|
31
|
+
| `add_shell(name, command, ...)` | Add a classic verb under `...\shell` |
|
|
32
|
+
| `add_handler(name, clsid, ...)` | Add a COM handler |
|
|
33
|
+
| `unblock(name_or_clsid)` | Unblock a blocked CLSID |
|
|
34
|
+
| `remove_shell(name, ...)` | Remove a classic verb |
|
|
35
|
+
| `remove_handler(name, ...)` | Remove a COM handler |
|
|
36
|
+
| `remove_shellnew(key_path)` | Remove an entry from the "New" submenu |
|
|
37
|
+
| `block(name_or_clsid)` | Block a built-in element by CLSID |
|
|
38
|
+
|
|
39
|
+
`scope`: `file`, `directory`, `background`, `folder`, `drive`, `allfiles`,
|
|
40
|
+
`all`, an extension (`.zip`), a ProgID glob (`VLC.*`), or `sfa:<glob>`.
|
|
41
|
+
|
|
42
|
+
Writing to `HKLM` (shared across users) requires running **as administrator**;
|
|
43
|
+
otherwise the action is reported as no-permission and continues.
|
|
44
|
+
|
|
45
|
+
## Examples
|
|
46
|
+
|
|
47
|
+
Everything is in the [`examples/`](examples) folder:
|
|
48
|
+
|
|
49
|
+
- [`examples/add.py`](examples/add.py) — add entries (open folder with VS Code,
|
|
50
|
+
PowerShell here, ...).
|
|
51
|
+
- [`examples/remove.py`](examples/remove.py) — full menu cleanup.
|
|
52
|
+
|
|
53
|
+
Copy one, tweak it to taste, and that's your per-PC script.
|
|
54
|
+
|
|
55
|
+
## `switchmenu` command
|
|
56
|
+
|
|
57
|
+
Toggle the right-click menu between the **classic** (Windows 10) and the **new**
|
|
58
|
+
(Windows 11) one. Restarts Explorer to apply instantly. No administrator needed:
|
|
59
|
+
everything lives under `HKEY_CURRENT_USER`.
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
switchmenu
|
|
63
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from windowsctxmenu import add_shell
|
|
2
|
+
|
|
3
|
+
add_shell(
|
|
4
|
+
"OpenFolderWithCode",
|
|
5
|
+
r'"C:\Program Files\Microsoft VS Code\Code.exe" "%V"',
|
|
6
|
+
scope="background",
|
|
7
|
+
label="Open Folder with Code",
|
|
8
|
+
icon=r"C:\Program Files\Microsoft VS Code\Code.exe",
|
|
9
|
+
)
|
|
10
|
+
add_shell(
|
|
11
|
+
"OpenWithCode",
|
|
12
|
+
r'"C:\Program Files\Microsoft VS Code\Code.exe" "%1"',
|
|
13
|
+
scope="file",
|
|
14
|
+
label="Open with Code",
|
|
15
|
+
icon=r"C:\Program Files\Microsoft VS Code\Code.exe",
|
|
16
|
+
)
|
|
17
|
+
add_shell(
|
|
18
|
+
"OpenPwshHere",
|
|
19
|
+
r'powershell.exe -NoExit -Command "Set-Location -LiteralPath \"%V\""',
|
|
20
|
+
scope="background",
|
|
21
|
+
label="Open PowerShell here",
|
|
22
|
+
icon="powershell.exe",
|
|
23
|
+
)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from windowsctxmenu import block, remove_handler, remove_shell, remove_shellnew
|
|
2
|
+
|
|
3
|
+
# Windows / drivers (HKLM -> admin)
|
|
4
|
+
remove_handler("NvCplDesktopContext", scope="background")
|
|
5
|
+
remove_shell("3D Print", scope=".3mf")
|
|
6
|
+
remove_handler("EPP")
|
|
7
|
+
remove_handler("Sharing")
|
|
8
|
+
remove_handler("Library Location", scope="folder")
|
|
9
|
+
remove_handler("SendTo", scope="allfiles")
|
|
10
|
+
remove_handler("Restore previous versions")
|
|
11
|
+
remove_shell("pintohomefile")
|
|
12
|
+
|
|
13
|
+
# Block by CLSID (restart Explorer afterwards)
|
|
14
|
+
block("Edit with Photos")
|
|
15
|
+
block("Share")
|
|
16
|
+
block("Edit in Notepad")
|
|
17
|
+
|
|
18
|
+
# 'Cast to Device' (PlayTo) everywhere it registers
|
|
19
|
+
remove_handler("PlayTo", scope="sfa:*")
|
|
20
|
+
remove_handler("PlayTo", scope="Stack.*")
|
|
21
|
+
|
|
22
|
+
# 'Set as desktop background' on image files
|
|
23
|
+
for ext in [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tif", ".tiff",
|
|
24
|
+
".heic", ".webp", ".dib", ".jfif"]:
|
|
25
|
+
remove_shell("setdesktopwallpaper", scope=ext)
|
|
26
|
+
|
|
27
|
+
# 'Print' verb everywhere
|
|
28
|
+
remove_shell("print", scope="*")
|
|
29
|
+
remove_shell("print", scope="sfa:*")
|
|
30
|
+
|
|
31
|
+
# 'New' submenu: prune everything except New Folder
|
|
32
|
+
for kp in [".contact", ".gdoc", ".gsheet", ".gslides",
|
|
33
|
+
".library-ms", ".lnk", ".zip\\CompressedFolder"]:
|
|
34
|
+
remove_shellnew(kp)
|
|
35
|
+
|
|
36
|
+
# User apps
|
|
37
|
+
for name in [
|
|
38
|
+
"WinDirStat",
|
|
39
|
+
"Open with Code",
|
|
40
|
+
"Add to VLC media player's Playlist",
|
|
41
|
+
"Play with VLC media player",
|
|
42
|
+
"Open Git Ba&sh here",
|
|
43
|
+
"Open Git &GUI here",
|
|
44
|
+
"Add to MPC-HC Playlist",
|
|
45
|
+
"Play with MPC-HC",
|
|
46
|
+
"Send with Tailscale...",
|
|
47
|
+
]:
|
|
48
|
+
remove_shell(name)
|
|
49
|
+
|
|
50
|
+
# VLC verbs across all its per-extension ProgIDs
|
|
51
|
+
remove_shell("AddToPlaylistVLC", scope="VLC.*")
|
|
52
|
+
remove_shell("PlayWithVLC", scope="VLC.*")
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "windowsctxmenu"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Add, remove and block Windows right-click context menu entries from Python."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{ name = "windowsctxmenu authors" }]
|
|
13
|
+
keywords = ["windows", "registry", "context-menu", "shell", "winreg"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Operating System :: Microsoft :: Windows",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
]
|
|
19
|
+
dependencies = []
|
|
20
|
+
|
|
21
|
+
[project.scripts]
|
|
22
|
+
switchmenu = "windowsctxmenu.cli:main"
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/offerrall/windowsctxmenu"
|
|
26
|
+
Repository = "https://github.com/offerrall/windowsctxmenu"
|
|
27
|
+
|
|
28
|
+
[tool.hatch.build.targets.wheel]
|
|
29
|
+
packages = ["src/windowsctxmenu"]
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
__version__ = "1.0.0"
|
|
2
|
+
|
|
3
|
+
from .addmenu import add_handler, add_shell, unblock
|
|
4
|
+
from .contextmenu import block, remove_handler, remove_shell, remove_shellnew
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"remove_shell",
|
|
8
|
+
"remove_handler",
|
|
9
|
+
"remove_shellnew",
|
|
10
|
+
"block",
|
|
11
|
+
"add_shell",
|
|
12
|
+
"add_handler",
|
|
13
|
+
"unblock",
|
|
14
|
+
]
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import winreg
|
|
2
|
+
|
|
3
|
+
from .utils import (
|
|
4
|
+
ACCESS,
|
|
5
|
+
BLOCKED_PATH,
|
|
6
|
+
CLASSES,
|
|
7
|
+
HIVES,
|
|
8
|
+
read_value,
|
|
9
|
+
resolve_clsid,
|
|
10
|
+
scope_bases,
|
|
11
|
+
set_value,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def add_shell(
|
|
16
|
+
name,
|
|
17
|
+
command,
|
|
18
|
+
scope="file",
|
|
19
|
+
label=None,
|
|
20
|
+
icon=None,
|
|
21
|
+
hive="HKCU",
|
|
22
|
+
extended=False,
|
|
23
|
+
):
|
|
24
|
+
root = HIVES[hive]
|
|
25
|
+
results = []
|
|
26
|
+
for base in scope_bases(root, scope):
|
|
27
|
+
rel = base + "\\shell\\" + name
|
|
28
|
+
path = CLASSES + rel
|
|
29
|
+
try:
|
|
30
|
+
set_value(root, path, "", label or name)
|
|
31
|
+
if icon:
|
|
32
|
+
set_value(root, path, "Icon", icon)
|
|
33
|
+
if extended:
|
|
34
|
+
set_value(root, path, "Extended", "")
|
|
35
|
+
set_value(root, path + "\\command", "", command)
|
|
36
|
+
results.append(f"added: [{hive}] {rel}")
|
|
37
|
+
except PermissionError:
|
|
38
|
+
results.append(f"no permission (run as admin): [{hive}] {rel}")
|
|
39
|
+
if not results:
|
|
40
|
+
results.append(f"no targets: {name} (scope={scope})")
|
|
41
|
+
return results
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def add_handler(name, clsid, scope="file", hive="HKCU"):
|
|
45
|
+
root = HIVES[hive]
|
|
46
|
+
results = []
|
|
47
|
+
for base in scope_bases(root, scope):
|
|
48
|
+
rel = base + "\\shellex\\ContextMenuHandlers\\" + name
|
|
49
|
+
path = CLASSES + rel
|
|
50
|
+
try:
|
|
51
|
+
set_value(root, path, "", clsid)
|
|
52
|
+
results.append(f"added: [{hive}] {rel}")
|
|
53
|
+
except PermissionError:
|
|
54
|
+
results.append(f"no permission (run as admin): [{hive}] {rel}")
|
|
55
|
+
if not results:
|
|
56
|
+
results.append(f"no targets: {name} (scope={scope})")
|
|
57
|
+
return results
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def unblock(name_or_clsid):
|
|
61
|
+
clsid = resolve_clsid(name_or_clsid)
|
|
62
|
+
if clsid is None:
|
|
63
|
+
return [f"unknown CLSID: {name_or_clsid!r}"]
|
|
64
|
+
results = []
|
|
65
|
+
for hive, root in HIVES.items():
|
|
66
|
+
try:
|
|
67
|
+
with winreg.OpenKey(
|
|
68
|
+
root, BLOCKED_PATH, 0, winreg.KEY_READ | winreg.KEY_WRITE | ACCESS
|
|
69
|
+
) as handle:
|
|
70
|
+
if read_value(handle, clsid) is None:
|
|
71
|
+
results.append(f"not blocked: [{hive}] {clsid} ({name_or_clsid})")
|
|
72
|
+
else:
|
|
73
|
+
winreg.DeleteValue(handle, clsid)
|
|
74
|
+
results.append(f"unblocked: [{hive}] {clsid} ({name_or_clsid})")
|
|
75
|
+
except FileNotFoundError:
|
|
76
|
+
results.append(f"not blocked: [{hive}] {clsid} ({name_or_clsid})")
|
|
77
|
+
except PermissionError:
|
|
78
|
+
results.append(f"no permission (run as admin): [{hive}] {clsid}")
|
|
79
|
+
return results
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""The ``switchmenu`` command: toggle the Windows 11 classic context menu."""
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
import time
|
|
5
|
+
import winreg
|
|
6
|
+
|
|
7
|
+
_KEY = r"Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32"
|
|
8
|
+
_PARENT = _KEY.rsplit("\\", 1)[0]
|
|
9
|
+
|
|
10
|
+
_ACCESS = winreg.KEY_WOW64_64KEY
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _is_classic_active() -> bool:
|
|
14
|
+
try:
|
|
15
|
+
with winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, _KEY, 0,
|
|
16
|
+
winreg.KEY_READ | _ACCESS) as k:
|
|
17
|
+
value, kind = winreg.QueryValueEx(k, "") # "" == the (Default) value
|
|
18
|
+
return kind == winreg.REG_SZ and value == ""
|
|
19
|
+
except FileNotFoundError:
|
|
20
|
+
return False
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _restart_explorer() -> None:
|
|
24
|
+
subprocess.run(["taskkill", "/f", "/im", "explorer.exe"],
|
|
25
|
+
check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
26
|
+
time.sleep(1)
|
|
27
|
+
subprocess.Popen(["explorer.exe"])
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def main() -> None:
|
|
31
|
+
if _is_classic_active():
|
|
32
|
+
winreg.DeleteKeyEx(winreg.HKEY_CURRENT_USER, _KEY, _ACCESS, 0)
|
|
33
|
+
try:
|
|
34
|
+
winreg.DeleteKeyEx(winreg.HKEY_CURRENT_USER, _PARENT, _ACCESS, 0)
|
|
35
|
+
except OSError:
|
|
36
|
+
# Parent has other subkeys or is already gone; that's fine.
|
|
37
|
+
pass
|
|
38
|
+
print("Switched to: new menu")
|
|
39
|
+
else:
|
|
40
|
+
with winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, _KEY, 0,
|
|
41
|
+
winreg.KEY_WRITE | _ACCESS) as k:
|
|
42
|
+
winreg.SetValueEx(k, "", 0, winreg.REG_SZ, "")
|
|
43
|
+
print("Switched to: classic menu")
|
|
44
|
+
|
|
45
|
+
_restart_explorer()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
if __name__ == "__main__":
|
|
49
|
+
main()
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import winreg
|
|
2
|
+
|
|
3
|
+
from .utils import (
|
|
4
|
+
ACCESS,
|
|
5
|
+
BLOCKED_PATH,
|
|
6
|
+
CLASSES,
|
|
7
|
+
HIVES,
|
|
8
|
+
SPECIAL_CLSIDS,
|
|
9
|
+
TYPE_SUFFIX,
|
|
10
|
+
delete_key_recursive,
|
|
11
|
+
read_value,
|
|
12
|
+
resolve_clsid,
|
|
13
|
+
scope_bases,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _find_child_name(root, shell_path, name):
|
|
18
|
+
try:
|
|
19
|
+
handle = winreg.OpenKey(root, shell_path, 0, winreg.KEY_READ | ACCESS)
|
|
20
|
+
except FileNotFoundError:
|
|
21
|
+
return None
|
|
22
|
+
target = name.lower()
|
|
23
|
+
try:
|
|
24
|
+
i = 0
|
|
25
|
+
while True:
|
|
26
|
+
try:
|
|
27
|
+
child = winreg.EnumKey(handle, i)
|
|
28
|
+
except OSError:
|
|
29
|
+
break
|
|
30
|
+
i += 1
|
|
31
|
+
if child.lower() == target:
|
|
32
|
+
return child
|
|
33
|
+
try:
|
|
34
|
+
sub = winreg.OpenKey(handle, child, 0, winreg.KEY_READ | ACCESS)
|
|
35
|
+
except OSError:
|
|
36
|
+
continue
|
|
37
|
+
try:
|
|
38
|
+
for vname in ("", "MUIVerb"):
|
|
39
|
+
label = read_value(sub, vname)
|
|
40
|
+
if label and label.lower() == target:
|
|
41
|
+
return child
|
|
42
|
+
finally:
|
|
43
|
+
winreg.CloseKey(sub)
|
|
44
|
+
finally:
|
|
45
|
+
winreg.CloseKey(handle)
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _find_handler_name(root, base_path, name):
|
|
50
|
+
try:
|
|
51
|
+
handle = winreg.OpenKey(root, base_path, 0, winreg.KEY_READ | ACCESS)
|
|
52
|
+
except FileNotFoundError:
|
|
53
|
+
return None
|
|
54
|
+
target = name.lower()
|
|
55
|
+
try:
|
|
56
|
+
i = 0
|
|
57
|
+
while True:
|
|
58
|
+
try:
|
|
59
|
+
child = winreg.EnumKey(handle, i)
|
|
60
|
+
except OSError:
|
|
61
|
+
break
|
|
62
|
+
i += 1
|
|
63
|
+
if child.lower() == target:
|
|
64
|
+
return child
|
|
65
|
+
finally:
|
|
66
|
+
winreg.CloseKey(handle)
|
|
67
|
+
return None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _remove(name, scope, kind):
|
|
71
|
+
suffix = TYPE_SUFFIX[kind]
|
|
72
|
+
results = []
|
|
73
|
+
found_any = False
|
|
74
|
+
for hive_name, root in HIVES.items():
|
|
75
|
+
for base in scope_bases(root, scope):
|
|
76
|
+
rel = base + "\\" + suffix
|
|
77
|
+
reg_path = CLASSES + rel
|
|
78
|
+
if kind == "handler":
|
|
79
|
+
real = _find_handler_name(root, reg_path, name)
|
|
80
|
+
else:
|
|
81
|
+
real = _find_child_name(root, reg_path, name)
|
|
82
|
+
if real is None:
|
|
83
|
+
continue
|
|
84
|
+
found_any = True
|
|
85
|
+
full = reg_path + "\\" + real
|
|
86
|
+
try:
|
|
87
|
+
if delete_key_recursive(root, full):
|
|
88
|
+
results.append(f"deleted: [{hive_name}] {rel}\\{real} ({name})")
|
|
89
|
+
except PermissionError:
|
|
90
|
+
results.append(
|
|
91
|
+
f"no permission (run as admin): [{hive_name}] {rel}\\{real} ({name})"
|
|
92
|
+
)
|
|
93
|
+
if not found_any:
|
|
94
|
+
results.append(f"not found: {name} (scope={scope}, type={kind})")
|
|
95
|
+
return results
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def remove_shell(name, scope="all"):
|
|
99
|
+
return _remove(name, scope, "shell")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def remove_handler(name, scope="all"):
|
|
103
|
+
target = SPECIAL_CLSIDS.get(name.strip().lower(), name)
|
|
104
|
+
return _remove(target, scope, "handler")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def remove_shellnew(key_path):
|
|
108
|
+
results = []
|
|
109
|
+
found_any = False
|
|
110
|
+
for hive_name, root in HIVES.items():
|
|
111
|
+
full = CLASSES + key_path + "\\ShellNew"
|
|
112
|
+
try:
|
|
113
|
+
winreg.CloseKey(winreg.OpenKey(root, full, 0, winreg.KEY_READ | ACCESS))
|
|
114
|
+
except FileNotFoundError:
|
|
115
|
+
continue
|
|
116
|
+
found_any = True
|
|
117
|
+
try:
|
|
118
|
+
if delete_key_recursive(root, full):
|
|
119
|
+
results.append(f"deleted ShellNew: [{hive_name}] {key_path}")
|
|
120
|
+
except PermissionError:
|
|
121
|
+
results.append(
|
|
122
|
+
f"no permission (run as admin): [{hive_name}] {key_path}\\ShellNew"
|
|
123
|
+
)
|
|
124
|
+
if not found_any:
|
|
125
|
+
results.append(f"not found: {key_path}\\ShellNew")
|
|
126
|
+
return results
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def block(name_or_clsid):
|
|
130
|
+
clsid = resolve_clsid(name_or_clsid)
|
|
131
|
+
if clsid is None:
|
|
132
|
+
catalog = ", ".join(sorted(SPECIAL_CLSIDS))
|
|
133
|
+
return [f"unknown CLSID: {name_or_clsid!r}. Pass a {{CLSID}} or: {catalog}."]
|
|
134
|
+
results = []
|
|
135
|
+
for hive_name, root in HIVES.items():
|
|
136
|
+
try:
|
|
137
|
+
with winreg.CreateKeyEx(
|
|
138
|
+
root, BLOCKED_PATH, 0, winreg.KEY_READ | winreg.KEY_WRITE | ACCESS
|
|
139
|
+
) as handle:
|
|
140
|
+
if read_value(handle, clsid) is not None:
|
|
141
|
+
results.append(f"already blocked: [{hive_name}] {clsid} ({name_or_clsid})")
|
|
142
|
+
else:
|
|
143
|
+
winreg.SetValueEx(handle, clsid, 0, winreg.REG_SZ, name_or_clsid)
|
|
144
|
+
results.append(f"blocked: [{hive_name}] {clsid} ({name_or_clsid})")
|
|
145
|
+
except PermissionError:
|
|
146
|
+
results.append(f"no permission (run as admin): [{hive_name}] {clsid} ({name_or_clsid})")
|
|
147
|
+
return results
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import fnmatch
|
|
2
|
+
import winreg
|
|
3
|
+
|
|
4
|
+
ACCESS = winreg.KEY_WOW64_64KEY
|
|
5
|
+
|
|
6
|
+
HIVES = {
|
|
7
|
+
"HKCU": winreg.HKEY_CURRENT_USER,
|
|
8
|
+
"HKLM": winreg.HKEY_LOCAL_MACHINE,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
CLASSES = "Software\\Classes\\"
|
|
12
|
+
|
|
13
|
+
TYPE_SUFFIX = {
|
|
14
|
+
"shell": "shell",
|
|
15
|
+
"handler": r"shellex\ContextMenuHandlers",
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
BLOCKED_PATH = r"Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Blocked"
|
|
19
|
+
|
|
20
|
+
SCOPE_BASES = {
|
|
21
|
+
"file": ["*"],
|
|
22
|
+
"directory": ["Directory"],
|
|
23
|
+
"background": [r"Directory\Background"],
|
|
24
|
+
"folder": ["Folder"],
|
|
25
|
+
"drive": ["Drive"],
|
|
26
|
+
"allfiles": ["AllFilesystemObjects"],
|
|
27
|
+
"image": [r"SystemFileAssociations\image"],
|
|
28
|
+
"video": [r"SystemFileAssociations\video"],
|
|
29
|
+
"audio": [r"SystemFileAssociations\audio"],
|
|
30
|
+
}
|
|
31
|
+
SCOPE_BASES["all"] = [
|
|
32
|
+
b
|
|
33
|
+
for k in ("file", "directory", "background", "folder", "drive", "allfiles")
|
|
34
|
+
for b in SCOPE_BASES[k]
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
SPECIAL_CLSIDS = {
|
|
38
|
+
"photos": "{BFE0E2A4-C70C-4AD7-AC3D-10D1ECEBB5B4}",
|
|
39
|
+
"edit with photos": "{BFE0E2A4-C70C-4AD7-AC3D-10D1ECEBB5B4}",
|
|
40
|
+
"share": "{E2BF9676-5F8F-435C-97EB-11607A5BEDF7}",
|
|
41
|
+
"give access to": "{f81e9010-6ea4-11ce-a7ff-00aa003ca9f6}",
|
|
42
|
+
"cast to device": "{7AD84985-87B4-4a16-BE58-8B72A5B390F7}",
|
|
43
|
+
"include in library": "{3dad6c5d-2167-4cae-9914-f99e41c12cfa}",
|
|
44
|
+
"restore previous versions": "{596AB062-B4D2-4215-9F74-E9109B0A8153}",
|
|
45
|
+
"scan with microsoft defender": "{09A47860-11B0-4DA5-AFA5-26D86198A780}",
|
|
46
|
+
"edit in notepad": "{CA6CC9F1-867A-481E-951E-A28C5E4F01EA}",
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def is_guid(text):
|
|
51
|
+
text = text.strip()
|
|
52
|
+
return text.startswith("{") and text.endswith("}") and len(text) == 38
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def resolve_clsid(name_or_clsid):
|
|
56
|
+
if is_guid(name_or_clsid):
|
|
57
|
+
return name_or_clsid
|
|
58
|
+
return SPECIAL_CLSIDS.get(name_or_clsid.strip().lower())
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def read_value(handle, value_name):
|
|
62
|
+
try:
|
|
63
|
+
value, _ = winreg.QueryValueEx(handle, value_name)
|
|
64
|
+
except OSError:
|
|
65
|
+
return None
|
|
66
|
+
return value if isinstance(value, str) else None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def set_value(root, path, value_name, value):
|
|
70
|
+
with winreg.CreateKeyEx(root, path, 0, winreg.KEY_WRITE | ACCESS) as handle:
|
|
71
|
+
winreg.SetValueEx(handle, value_name, 0, winreg.REG_SZ, value)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def match_subkeys(root, base_path, pattern):
|
|
75
|
+
try:
|
|
76
|
+
handle = winreg.OpenKey(root, base_path, 0, winreg.KEY_READ | ACCESS)
|
|
77
|
+
except FileNotFoundError:
|
|
78
|
+
return []
|
|
79
|
+
pat = pattern.lower()
|
|
80
|
+
names = []
|
|
81
|
+
try:
|
|
82
|
+
i = 0
|
|
83
|
+
while True:
|
|
84
|
+
try:
|
|
85
|
+
child = winreg.EnumKey(handle, i)
|
|
86
|
+
except OSError:
|
|
87
|
+
break
|
|
88
|
+
i += 1
|
|
89
|
+
if fnmatch.fnmatch(child.lower(), pat):
|
|
90
|
+
names.append(child)
|
|
91
|
+
finally:
|
|
92
|
+
winreg.CloseKey(handle)
|
|
93
|
+
return names
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def scope_bases(root, scope):
|
|
97
|
+
if scope.startswith("sfa:"):
|
|
98
|
+
return [
|
|
99
|
+
"SystemFileAssociations\\" + n
|
|
100
|
+
for n in match_subkeys(root, CLASSES + "SystemFileAssociations", scope[4:])
|
|
101
|
+
]
|
|
102
|
+
if scope.startswith("."):
|
|
103
|
+
return ["SystemFileAssociations\\" + scope]
|
|
104
|
+
if "*" in scope or "?" in scope:
|
|
105
|
+
return match_subkeys(root, CLASSES.rstrip("\\"), scope)
|
|
106
|
+
try:
|
|
107
|
+
return SCOPE_BASES[scope]
|
|
108
|
+
except KeyError:
|
|
109
|
+
valid = ", ".join(sorted(SCOPE_BASES))
|
|
110
|
+
raise ValueError(
|
|
111
|
+
f"Invalid scope: {scope!r}. Valid: {valid}, '.ext', 'VLC.*', 'sfa:<glob>'."
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def delete_key_recursive(root, subkey):
|
|
116
|
+
try:
|
|
117
|
+
handle = winreg.OpenKey(
|
|
118
|
+
root, subkey, 0, winreg.KEY_READ | winreg.KEY_WRITE | ACCESS
|
|
119
|
+
)
|
|
120
|
+
except FileNotFoundError:
|
|
121
|
+
return False
|
|
122
|
+
try:
|
|
123
|
+
while True:
|
|
124
|
+
try:
|
|
125
|
+
child = winreg.EnumKey(handle, 0)
|
|
126
|
+
except OSError:
|
|
127
|
+
break
|
|
128
|
+
delete_key_recursive(handle, child)
|
|
129
|
+
finally:
|
|
130
|
+
winreg.CloseKey(handle)
|
|
131
|
+
winreg.DeleteKeyEx(root, subkey, ACCESS, 0)
|
|
132
|
+
return True
|