Created
October 29, 2017 20:05
-
-
Save vbuaraujo/418951e1eae8a5d0ff5059a25a416e5d to your computer and use it in GitHub Desktop.
Pass on desktop notifications to Emacs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Pass on desktop notifications to Emacs. | |
# Based on https://askubuntu.com/questions/89279/listening-to-incoming-libnotify-notifications-using-dbus/451402#451402 | |
import glib | |
import dbus | |
from dbus.mainloop.glib import DBusGMainLoop | |
from subprocess import call | |
# [dbus.String(u'theapp'), dbus.UInt32(0L), dbus.String(u'icon'), dbus.String(u'summary'), dbus.String(u'body'), dbus.Array([], signature=dbus.Signature('s')), dbus.Dictionary({dbus.String(u'category'): dbus.String(u'category', variant_level=1), dbus.String(u'urgency'): dbus.Byte(0, variant_level=1)}, signature=dbus.Signature('sv')), dbus.Int32(-1)] | |
def escape(string): | |
return string.replace('\\', '\\\\').replace('"', r'\"') | |
def notifications(bus, message): | |
args = message.get_args_list() | |
try: | |
app, _, icon, summary, body, _, _, _ = args | |
except Exception as exc: | |
return | |
elisp_call='(message "{}: {}")'.format( | |
escape(summary), | |
escape(body)) | |
call(["emacsclient", "-e", elisp_call]) | |
DBusGMainLoop(set_as_default=True) | |
bus = dbus.SessionBus() | |
bus.add_match_string_non_blocking("eavesdrop=true, interface='org.freedesktop.Notifications', member='Notify'") | |
bus.add_message_filter(notifications) | |
mainloop = glib.MainLoop() | |
mainloop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment