From ec71acf498cf4d5070edea3022e4a6cea57758e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Thu, 23 Jan 2025 11:12:21 +0100 Subject: [PATCH] feat: added terminal-notifier fallback on macOS when running unsigned --- aw_notify/main.py | 62 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/aw_notify/main.py b/aw_notify/main.py index 57a3f85..e77396f 100644 --- a/aw_notify/main.py +++ b/aw_notify/main.py @@ -4,6 +4,8 @@ """ import logging +import shutil +import subprocess import sys import threading from collections import defaultdict @@ -142,17 +144,61 @@ def to_hms(duration: timedelta) -> str: def notify(title: str, msg: str): """send a notification to the user""" - global notifier - if notifier is None: - notifier = DesktopNotifier( - app_name="AW", - app_icon=f"file://{icon_path}", - notification_limit=10, - ) logger.info(f'Showing: "{title} - {msg}"') - notifier.send_sync(title=title, message=msg) + + # Try terminal-notifier first on macOS + if sys.platform == "darwin": + if notify_terminal_notifier(title, msg): + return + + # Fall back to desktop-notifier + try: + if notifier is None: + notifier = DesktopNotifier( + app_name="AW", + app_icon=f"file://{icon_path}", + notification_limit=10, + ) + notifier.send_sync(title=title, message=msg) + return + except Exception as e: + logger.info(f"desktop-notifier not used: {e}") + + # If all notification methods fail, log a warning + logger.warning("All notification methods failed") + + +def notify_terminal_notifier(title: str, msg: str) -> bool: + """Send notification using terminal-notifier. Returns True if successful.""" + if not shutil.which("terminal-notifier"): + return False + try: + result = subprocess.run( + [ + "terminal-notifier", + "-title", + "ActivityWatch", + "-subtitle", + title, + "-message", + msg.strip("-").replace("- ", ", ").replace("\n", ""), + "-appIcon", + str(icon_path), + "-group", + title, + "-open", + "http://localhost:5600", + ], + capture_output=True, + text=True, + check=True, + ) + return result.returncode == 0 + except Exception as e: + logger.warning(f"Failed to send notification using terminal-notifier: {e}") + return False class CategoryAlert: