161 lines
5.1 KiB
Python
161 lines
5.1 KiB
Python
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def _exe_path() -> str:
|
|
return sys.executable if getattr(sys, "frozen", False) else ""
|
|
|
|
|
|
def _is_frozen() -> bool:
|
|
return getattr(sys, "frozen", False)
|
|
|
|
|
|
# ── Autostart ─────────────────────────────────────────────────────────────────
|
|
|
|
def _autostart_path() -> str:
|
|
if sys.platform == "win32":
|
|
return ""
|
|
return os.path.join(os.path.expanduser("~"), ".config", "autostart",
|
|
"whisper-dictation.desktop")
|
|
|
|
|
|
def autostart_installed() -> bool:
|
|
if sys.platform == "win32":
|
|
import winreg
|
|
try:
|
|
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
|
|
r"Software\Microsoft\Windows\CurrentVersion\Run")
|
|
winreg.QueryValueEx(key, "WhisperDictation")
|
|
return True
|
|
except FileNotFoundError:
|
|
return False
|
|
return os.path.exists(_autostart_path())
|
|
|
|
|
|
def install_autostart() -> None:
|
|
if sys.platform == "win32":
|
|
import winreg
|
|
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
|
|
r"Software\Microsoft\Windows\CurrentVersion\Run",
|
|
access=winreg.KEY_SET_VALUE)
|
|
winreg.SetValueEx(key, "WhisperDictation", 0, winreg.REG_SZ, f'"{_exe_path()}"')
|
|
else:
|
|
path = _autostart_path()
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
_write_desktop(path)
|
|
|
|
|
|
def remove_autostart() -> None:
|
|
if sys.platform == "win32":
|
|
import winreg
|
|
try:
|
|
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
|
|
r"Software\Microsoft\Windows\CurrentVersion\Run",
|
|
access=winreg.KEY_SET_VALUE)
|
|
winreg.DeleteValue(key, "WhisperDictation")
|
|
except FileNotFoundError:
|
|
pass
|
|
else:
|
|
path = _autostart_path()
|
|
if os.path.exists(path):
|
|
os.remove(path)
|
|
|
|
|
|
# ── Start menu ────────────────────────────────────────────────────────────────
|
|
|
|
def _startmenu_path() -> str:
|
|
if sys.platform == "win32":
|
|
return os.path.join(os.environ.get("APPDATA", ""),
|
|
r"Microsoft\Windows\Start Menu\Programs\Whisper Dictation.lnk")
|
|
return os.path.join(os.path.expanduser("~"), ".local", "share", "applications",
|
|
"whisper-dictation.desktop")
|
|
|
|
|
|
def startmenu_installed() -> bool:
|
|
return os.path.exists(_startmenu_path())
|
|
|
|
|
|
def install_startmenu() -> None:
|
|
path = _startmenu_path()
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
if sys.platform == "win32":
|
|
_create_lnk(path)
|
|
else:
|
|
_write_desktop(path)
|
|
|
|
|
|
def remove_startmenu() -> None:
|
|
path = _startmenu_path()
|
|
if os.path.exists(path):
|
|
os.remove(path)
|
|
|
|
|
|
# ── Desktop shortcut ──────────────────────────────────────────────────────────
|
|
|
|
def _desktop_dir() -> str:
|
|
if sys.platform == "win32":
|
|
return os.environ.get("USERPROFILE", os.path.expanduser("~"))
|
|
try:
|
|
result = subprocess.run(["xdg-user-dir", "DESKTOP"], capture_output=True, text=True)
|
|
path = result.stdout.strip()
|
|
if path:
|
|
return path
|
|
except FileNotFoundError:
|
|
pass
|
|
return os.path.join(os.path.expanduser("~"), "Desktop")
|
|
|
|
|
|
def _desktop_path() -> str:
|
|
if sys.platform == "win32":
|
|
return os.path.join(_desktop_dir(), "Whisper Dictation.lnk")
|
|
return os.path.join(_desktop_dir(), "whisper-dictation.desktop")
|
|
|
|
|
|
def desktop_installed() -> bool:
|
|
return os.path.exists(_desktop_path())
|
|
|
|
|
|
def install_desktop() -> None:
|
|
path = _desktop_path()
|
|
if sys.platform == "win32":
|
|
_create_lnk(path)
|
|
else:
|
|
_write_desktop(path)
|
|
os.chmod(path, 0o755)
|
|
|
|
|
|
def remove_desktop() -> None:
|
|
path = _desktop_path()
|
|
if os.path.exists(path):
|
|
os.remove(path)
|
|
|
|
|
|
# ── Helpers ───────────────────────────────────────────────────────────────────
|
|
|
|
def _write_desktop(path: str) -> None:
|
|
icon_path = os.path.join(os.path.dirname(_exe_path()), "icon.png")
|
|
content = (
|
|
"[Desktop Entry]\n"
|
|
"Type=Application\n"
|
|
"Name=Whisper Dictation\n"
|
|
f"Exec={_exe_path()}\n"
|
|
f"Icon={icon_path}\n"
|
|
"Terminal=false\n"
|
|
"Categories=Utility;\n"
|
|
)
|
|
with open(path, "w") as f:
|
|
f.write(content)
|
|
|
|
|
|
def _create_lnk(path: str) -> None:
|
|
if sys.platform != "win32":
|
|
return
|
|
import win32com.client
|
|
shell = win32com.client.Dispatch("WScript.Shell")
|
|
lnk = shell.CreateShortCut(path)
|
|
lnk.Targetpath = _exe_path()
|
|
lnk.WorkingDirectory = os.path.dirname(_exe_path())
|
|
lnk.IconLocation = _exe_path()
|
|
lnk.save()
|