35 lines
1 KiB
Python
35 lines
1 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
if sys.platform == "win32":
|
|
import winreg
|
|
|
|
|
|
RUN_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run"
|
|
|
|
|
|
def set_startup_enabled(app_name: str, enabled: bool, command: str) -> None:
|
|
if sys.platform != "win32":
|
|
return
|
|
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY, 0, winreg.KEY_SET_VALUE) as key:
|
|
if enabled:
|
|
winreg.SetValueEx(key, app_name, 0, winreg.REG_SZ, command)
|
|
else:
|
|
try:
|
|
winreg.DeleteValue(key, app_name)
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
|
|
def build_startup_command() -> str:
|
|
if getattr(sys, "frozen", False):
|
|
exe = Path(sys.executable).resolve()
|
|
return f'"{exe}" --tray'
|
|
|
|
base_dir = Path(__file__).resolve().parents[1]
|
|
script = base_dir / "main.py"
|
|
venv_pythonw = base_dir / ".venv" / "Scripts" / "pythonw.exe"
|
|
python_exe = venv_pythonw if venv_pythonw.exists() else Path(sys.executable).resolve()
|
|
return f'"{python_exe}" "{script}" --tray'
|