5

I searched quiet a bit but all the articles and answers only tell you how to create your own notification. What I want to do is to keep track of all the notifications made by all the applications, and execute a particular script every time a particular notification is made.

I will write my program to do this. All I need help with is how to 'intercept' the notifications.

Jacob Vlijm
  • 83,767
  • Everytime a particular application makes a notification, or everytime a particular notification appears? – Jacob Vlijm Nov 13 '15 at 18:43
  • Everytime a particular application makes a notification. Though I will still need the contents of the notification. – piyushrungta25 Nov 13 '15 at 18:47
  • 2
    By editing the (very nice) answer below: dbus-monitor "interface='org.freedesktop.Notifications'" | grep --line-buffered "string" | grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v | grep --line-buffered '.*(?=string)|(?<=string).*' -oPi | grep --line-buffered -v '^\s*$' | xargs -I '{}' echo {} > file.txt we can write the notifications to (e.g.) a file or do anything else with it, like triggering an action if the message contains or matches a certain string. I don't see any options for catching the pid which sent the message however, but it might be worth looking into. – Jacob Vlijm Nov 13 '15 at 18:59
  • The link to the answer I am referring to above: http://askubuntu.com/questions/368611/is-there-a-way-to-make-ubuntu-read-out-notifications (ran out of comment- space) – Jacob Vlijm Nov 13 '15 at 19:00
  • Interesting question though. – Jacob Vlijm Nov 13 '15 at 19:01
  • Just what I wanted. Getting pid won't be necessary. Many thanks. If you post this below, I will mark it as answer. Also i wanted to know, how did you find the other answer because I searched before asking here and I couldn't find it. – piyushrungta25 Nov 14 '15 at 09:45
  • Posted my answer, glad it was useful :) – Jacob Vlijm Nov 14 '15 at 16:07
  • I found the linked post a few days ago, by coincidence. I had no use for it, but was quite fond of it for the new insights I got from it. When I ran into your question, it was "fresh enough" to remember where and how I could find it :) – Jacob Vlijm Nov 14 '15 at 16:23

1 Answers1

5

Use (and identify) notifications to trigger subsequent actions

If we edit the proposed snippet in this very nice answer a bit, we can write the called notification to a file:

dbus-monitor "interface='org.freedesktop.Notifications'" | grep --line-buffered "string" | grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v | grep --line-buffered '.*(?=string)|(?<=string).*' -oPi | grep --line-buffered -v '^\s*$' | xargs -I '{}' echo {} > file.txt

or otherwise use it to trigger subsequent actions.

An example

If we edit the snippet to run a script when a notification pops up:

dbus-monitor "interface='org.freedesktop.Notifications'" | grep --line-buffered "string" | grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v | grep --line-buffered '.*(?=string)|(?<=string).*' -oPi | grep --line-buffered -v '^\s*$' | xargs -I '{}' /bin/bash /path/to/script.sh {}

and script.sh is:

#!/bin/bash
string=$1
match="een aap op een fiets"
if [[ $string == $match ]]
  then
    gedit
fi

Then, every time, if the notification matches "een aap op een fiets":

enter image description here

gedit will open :)

enter image description here

Note

Although the code works perfectly to intercept the notification to trigger any kind of action, I found no way to identify the pid that called the notification.

Jacob Vlijm
  • 83,767