Execute a script every time a particular application makes a notification

Solution 1:

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.