Action buttons in notifications.
I was wondering if I could have a desktop notification with notify-send for my XFCE Genmon script. My usual approach to scripting failed as the answer to the question at the usual scripting question sites seemed to be "It's not possible".
However, the manual says it is possible.
OPTIONS
-A, --action=[NAME=]Text...
Specifies the actions to display to the user. Implies --wait to
wait for user input. May be set multiple times. The NAME of the
action is output to stdout. If NAME is not specified, the numerical
index of the option is used (starting with 1).
This action was added about a year ago.
https://gitlab.gnome.org/GNOME/libnotif ... equests/18
After a few attempts with the method from the cartoon, I managed to put together a working script. I'm posting it here so anybody searching for "Using notify-send, is there a way to associate a notification with some action?" can find a working example, not "It seems notify-send can't do this", or "No, notify-send doesn't support the use of actions/buttons", which isn't true any more.
Action buttons return a specified output (or an output of 0, 1... if no output text is specified). If they are not clicked, output is null. Thus
Code: Select all
--action=yes=Okay --action=no=Cancel
Returns "yes", "no" or null.
Returns "0", "1" or null.
Output can be redirected to a text file:
Code: Select all
#!/bin/bash
notify-send --icon=system-software-update -w --action=yes="Click to update" "Updates available" > output.txt
action=$(cat output.txt)
if [ "$action" == "yes" ]; then
synaptic-pkexec
fi
or to a function:
Code: Select all
#!/bin/bash
action=$(notify-send --icon=system-software-update -w --action=yes="Click to update" "Updates available")
if [ "$action" == "yes" ]; then
synaptic-pkexec
fi
which seems like a better way to do it.
To have the number of updates appear in the notification:
Code: Select all
#!/bin/bash
updates=$(aptitude search '~U' | wc -l)
action=$(notify-send --icon=system-software-update -w --action=yes="Click to update" "$updates Updates available")
if [ "$action" == "yes" ]; then
synaptic-pkexec
fi
Used in the Genmon script, you get a permanent reminder in the panel if an update is not done, and a reminder/record of the notification from the notification plugin.
![Image](https://i.postimg.cc/WqV1LHsM/notification1.png)