Files
2026-10-16 15:55:14 +02:00

57 lines
1.8 KiB
Python

"""Autostart beim Anmelden (Windows: Registry Run-Key, macOS: LaunchAgent, Linux: XDG-Autostart)."""
import os
import plistlib
import shlex
import sys
from pathlib import Path
from .config import SYSTEM
LABEL = "de.netclip.client"
def command() -> list[str]:
if getattr(sys, "frozen", False):
return [sys.executable, "--minimized"]
return [sys.executable, "-m", "netclip", "--minimized"]
def _linux_file() -> Path:
base = Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config"))
return base / "autostart" / "netclip.desktop"
def _mac_file() -> Path:
return Path.home() / "Library" / "LaunchAgents" / f"{LABEL}.plist"
def set_enabled(enabled: bool):
cmd = command()
if SYSTEM == "Windows":
import winreg
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Run",
0, winreg.KEY_SET_VALUE)
with key:
if enabled:
winreg.SetValueEx(key, "NetClip", 0, winreg.REG_SZ, " ".join(f'"{c}"' for c in cmd))
else:
try:
winreg.DeleteValue(key, "NetClip")
except FileNotFoundError:
pass
elif SYSTEM == "Darwin":
f = _mac_file()
if enabled:
f.parent.mkdir(parents=True, exist_ok=True)
f.write_bytes(plistlib.dumps({"Label": LABEL, "ProgramArguments": cmd, "RunAtLoad": True}))
else:
f.unlink(missing_ok=True)
else:
f = _linux_file()
if enabled:
f.parent.mkdir(parents=True, exist_ok=True)
f.write_text("[Desktop Entry]\nType=Application\nName=NetClip\n"
f"Exec={shlex.join(cmd)}\nIcon=edit-paste\nX-GNOME-Autostart-enabled=true\n")
else:
f.unlink(missing_ok=True)