#!/usr/bin/env python3 """События Calendar и напоминания Reminders на сегодня.""" from __future__ import annotations import argparse import json import subprocess import sys from pathlib import Path CALENDAR_SCRIPT = """ set output to "" set dayStart to current date set hours of dayStart to 0 set minutes of dayStart to 0 set seconds of dayStart to 0 set dayEnd to dayStart + (1 * days) tell application "Calendar" repeat with cal in calendars try set matched to (every event of cal whose start date is greater than or equal to dayStart and start date is less than dayEnd) repeat with ev in matched set output to output & (name of cal) & tab & (summary of ev) & tab & (start date of ev as string) & linefeed end repeat end try end repeat end tell return output """ REMINDERS_SCRIPT = """ set output to "" tell application "Reminders" repeat with listRef in lists try repeat with rem in reminders of listRef if completed of rem is false then set dueText to "" try set dueText to (due date of rem as string) end try set output to output & (name of listRef) & tab & (name of rem) & tab & dueText & linefeed end if end repeat end try end repeat end tell return output """ def osascript(script: str) -> tuple[int, str]: result = subprocess.run( ["osascript", "-e", script], capture_output=True, text=True, check=False, ) return result.returncode, (result.stdout if result.returncode == 0 else result.stderr) def rows(text: str, fields: int) -> list[list[str]]: result = [] for line in text.splitlines(): if not line.strip(): continue parts = line.split("\t") if len(parts) < fields: parts += [""] * (fields - len(parts)) result.append(parts[:fields]) return result def main() -> int: parser = argparse.ArgumentParser(description="События и напоминания на сегодня") parser.add_argument("--calendar", action="store_true", help="Только календарь") parser.add_argument("--reminders", action="store_true", help="Только напоминания") parser.add_argument("--json", type=Path) args = parser.parse_args() show_calendar = args.calendar or not args.reminders show_reminders = args.reminders or not args.calendar report = {"calendar": [], "reminders": []} errors = [] if show_calendar: code, output = osascript(CALENDAR_SCRIPT) if code == 0: report["calendar"] = rows(output, 3) else: errors.append(f"Calendar: {output.strip()}") if show_reminders: code, output = osascript(REMINDERS_SCRIPT) if code == 0: report["reminders"] = rows(output, 3) else: errors.append(f"Reminders: {output.strip()}") if args.json: args.json.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") if show_calendar: print(f"События на сегодня: {len(report['calendar'])}") for calendar, summary, start in report["calendar"]: print(f" {start} {summary} [{calendar}]") if show_reminders: print(f"\nНезавершённые напоминания: {len(report['reminders'])}") for list_name, name, due in report["reminders"]: suffix = f" (срок: {due})" if due else "" print(f" {name}{suffix} [{list_name}]") for error in errors: print(f"Ошибка: {error}", file=sys.stderr) if errors: print("Разрешите автоматизацию Calendar и Reminders в Системных настройках.", file=sys.stderr) return 2 return 0 if __name__ == "__main__": raise SystemExit(main())