notifications.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python
  2. # A sample script to process notifications. Save it as
  3. # ~/.config/kitty/notifications.py
  4. import subprocess
  5. from kitty.notifications import NotificationCommand, Urgency
  6. def log_notification(nc: NotificationCommand) -> None:
  7. # Log notifications to /tmp/notifications-log.txt
  8. with open('/tmp/notifications-log.txt', 'a') as log:
  9. print(f'title: {nc.title}', file=log)
  10. print(f'body: {nc.body}', file=log)
  11. print(f'app: {nc.application_name}', file=log)
  12. print(f'types: {nc.notification_types}', file=log)
  13. print('\n', file=log)
  14. def on_notification_activated(nc: NotificationCommand, which: int) -> None:
  15. # do something when this notification is activated (clicked on)
  16. # remember to assign this to the on_activation field in main()
  17. pass
  18. def main(nc: NotificationCommand) -> bool:
  19. '''
  20. This function should return True to filter out the notification
  21. '''
  22. log_notification(nc)
  23. # filter out notifications with 'unwanted' in their titles
  24. if 'unwanted' in nc.title.lower():
  25. return True
  26. # force the notification to be silent
  27. nc.sound_name = 'silent'
  28. # filter out notifications from the application badapp
  29. if nc.application_name == 'badapp':
  30. return True
  31. # filter out low urgency notifications
  32. if nc.urgency is Urgency.Low:
  33. return True
  34. # replace some bad text in the notification body
  35. nc.body = nc.body.replace('bad text', 'good text')
  36. # run a script if this notification is from myapp and has
  37. # type foo, passing in the title and body as command line args
  38. # to the script.
  39. if nc.application_name == 'myapp' and 'foo' in nc.notification_types:
  40. subprocess.Popen(['/path/to/my/script', nc.title, nc.body])
  41. # do some arbitrary actions when this notification is activated
  42. nc.on_activation = on_notification_activated
  43. # dont filter out this notification
  44. return False