Files
SyncVito/src/syncvito/autostart.py
T

32 lines
1.0 KiB
Python

"""Verwaltung des benutzerspezifischen Windows-Autostarts."""
from __future__ import annotations
import os
import sys
from contextlib import suppress
from pathlib import Path
__all__ = ["set_autostart"]
def set_autostart(enabled: bool) -> None:
"""Setzt oder entfernt SyncVito unter HKCU\\...\\Run."""
if os.name != "nt":
return
import winreg
key_path = r"Software\Microsoft\Windows\CurrentVersion\Run"
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path, 0, winreg.KEY_SET_VALUE) as key:
if enabled:
executable = Path(sys.executable)
if getattr(sys, "frozen", False):
command = f'"{executable}" --minimized'
else:
entry = Path(__file__).resolve().parents[2] / "main.py"
command = f'"{executable}" "{entry}" --minimized'
winreg.SetValueEx(key, "SyncVito", 0, winreg.REG_SZ, command)
else:
with suppress(FileNotFoundError):
winreg.DeleteValue(key, "SyncVito")