Skip to content

Instantly share code, notes, and snippets.

@foriequal0
Last active June 11, 2024 07:44
Show Gist options
  • Save foriequal0/7ef88eb7c47bee6da696a426e8a00df6 to your computer and use it in GitHub Desktop.
Save foriequal0/7ef88eb7c47bee6da696a426e8a00df6 to your computer and use it in GitHub Desktop.
Organize tray icons of Windows 11
from dataclasses import dataclass
import re
import os
import sys
import winreg
from win32comext.shell import shell
@dataclass
class NotifyIconSetting:
Key: str
ExecutablePath: str
# IconSnapshot: bytes
# IconGuid: str
InitialTooltip: str
Publisher: str
IsPromoted: int
# UID: int
def collect():
NOTIFY_ICON_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Control Panel\\NotifyIconSettings")
(subkeys_count, *_) = winreg.QueryInfoKey(NOTIFY_ICON_SETTINGS)
settings = []
for subkey_index in range(subkeys_count):
subkey_str = winreg.EnumKey(NOTIFY_ICON_SETTINGS, subkey_index)
subkey = winreg.OpenKey(NOTIFY_ICON_SETTINGS, subkey_str)
(_, values_count, _) = winreg.QueryInfoKey(subkey)
d = dict()
for value_index in range(values_count):
(name, value, type) = winreg.EnumValue(subkey, value_index)
d[name] = value
settings.append(NotifyIconSetting(
Key = subkey_str,
ExecutablePath=d.get("ExecutablePath"),
# IconSnapshot=d.get("IconSnapshot"),
# IconGuid=d.get("IconGuid"),
InitialTooltip=d.get("InitialTooltip"),
Publisher=d.get("Publisher"),
IsPromoted=d.get("IsPromoted"),
# UID=d.get("UID")
))
settings.sort(key=lambda x: f"{x.Publisher} | {x.InitialTooltip} | {x.ExecutablePath}")
return settings
def remove(settings):
notify_icon_settings = winreg.OpenKey(winreg.HKEY_CURRENT_USER, f"Control Panel\\NotifyIconSettings")
for setting in settings:
print(setting)
winreg.DeleteKey(notify_icon_settings, setting.Key)
clsid_pat = re.compile(r"\{........-....-....-....-............}", re.ASCII)
def executable_exists(path: str) -> bool:
def replace(clsid: re.Match[str]) -> str:
res = shell.SHGetKnownFolderPath(clsid.group(0), 0, None)
return res
replaced = clsid_pat.sub(replace, path)
return os.path.isfile(replaced)
def do_cleanup():
settings = collect()
settings, expired = splice(lambda x: executable_exists(x.ExecutablePath), settings)
print("===== remaining")
for item in settings:
print(item)
print("===== removing (expired)")
for item in expired:
print(item)
remove(expired)
def splice(pred, iter) -> (list, list):
x = list()
y = list()
for item in iter:
if pred(item):
x.append(item)
else:
y.append(item)
return x, y
def distinct_splice(iter, /, key) -> (list, list):
distinct = list()
duplicated = list()
found = set()
for item in iter:
k = key(item)
if k not in found:
found.add(k)
distinct.append(item)
else:
duplicated.append(item)
return distinct, duplicated
def do_dedup_by_name():
settings = collect()
nonexpired, _ = splice(lambda x: executable_exists(x.ExecutablePath), settings)
distinct, duplicated = distinct_splice(nonexpired, key=lambda x: os.path.basename(x.ExecutablePath))
print("===== remaining")
for item in nonexpired + distinct:
print(item)
print("===== removing (duplicated)")
for item in duplicated:
print(item)
remove(duplicated)
def do_nuke():
settings = collect()
settings, non_promoted = splice(lambda x: x.IsPromoted == 1, settings)
print("===== remaining")
for item in settings:
print(item)
print("===== removing (non-promoted)")
for item in non_promoted:
print(item)
remove(non_promoted)
def main():
command = None
if len(sys.argv) >= 2:
command = sys.argv[1]
if command == 'cleanup':
do_cleanup()
elif command == 'dedup':
do_dedup_by_name()
elif command == 'nuke':
do_nuke()
else:
if command != 'help':
print("Unknown command:", command)
print(f"Usage: python3 {sys.argv[0]} <cleanup|dedup|nuke>")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment