Spaces:
Running
Running
| """ | |
| modules/app_scheduler.py — Phase 4: Scheduled app launching (Windows Task Scheduler). | |
| Creates a one-time task using schtasks for the current user. | |
| """ | |
| from __future__ import annotations | |
| import datetime as _dt | |
| import subprocess | |
| from dataclasses import dataclass | |
| class SchedResult: | |
| ok: bool | |
| message: str | |
| def schedule_run(task_name: str, exe: str, when: _dt.datetime) -> SchedResult: | |
| tn = (task_name or "").strip() or "FRIDAY_RunApp" | |
| cmd = (exe or "").strip() | |
| if not cmd: | |
| return SchedResult(False, "Missing executable.") | |
| # schtasks expects HH:MM (24h) and date MM/DD/YYYY | |
| st = when.strftime("%H:%M") | |
| sd = when.strftime("%m/%d/%Y") | |
| p = subprocess.run( | |
| ["schtasks", "/Create", "/SC", "ONCE", "/TN", tn, "/TR", cmd, "/ST", st, "/SD", sd, "/F"], | |
| capture_output=True, | |
| text=True, | |
| shell=False, | |
| ) | |
| out = (p.stdout or p.stderr or "").strip() | |
| if p.returncode == 0: | |
| return SchedResult(True, f"Scheduled {cmd} at {when}.") | |
| if "access is denied" in out.lower(): | |
| return SchedResult(False, "Scheduling needs admin permission on this system.") | |
| return SchedResult(False, f"Schedule failed: {out[:200]}") | |
| def cancel(task_name: str) -> SchedResult: | |
| tn = (task_name or "").strip() | |
| if not tn: | |
| return SchedResult(False, "Missing task name.") | |
| p = subprocess.run(["schtasks", "/Delete", "/TN", tn, "/F"], capture_output=True, text=True, shell=False) | |
| out = (p.stdout or p.stderr or "").strip() | |
| if p.returncode == 0: | |
| return SchedResult(True, f"Cancelled task {tn}.") | |
| return SchedResult(False, f"Cancel failed: {out[:200]}") | |