Monday, January 2, 2023

Action buttons in notify-send notifications

Search for "action button notify-send", and the first two results currently say "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.

This action was added about a year ago.

gitlab.gnome.org

From the manual:

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).

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

--action=yes=Okay --action=no=Cancel

Returns "yes", "no" or null.

--action=Okay --action=Cancel

Returns "0", "1" or null.

Output can be redirected to a text file and used to run a command:


#!/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:


#!/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 in Debian:

 #!/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

This can be used in my update notification Genmon script for XFCE.

 

Update: If used with Genmon script, add the following to refresh the Genmon panel notification after the update:

if [ "$action" == "yes" ]; then
synaptic-pkexec && sleep 2 && xfce4-panel --plugin-event=genmon-<n>:refresh:bool:true
fi

Where <n> is the Genmon ID number obtained by hovering over the Genmon item item in Panel Preferences > Items.

No comments:

Post a Comment