scripting environment values #548
-
I am trying to write a script playing sounds when a notification pops up. I want to customize its behaviour based on the notification und have found these environment variables in this file. public async bool run_script (NotifyParams param, out string msg) {
string[] spawn_env = {};
spawn_env += "SWAYNC_APP_NAME=%s".printf (param.app_name);
spawn_env += "SWAYNC_SUMMARY=%s".printf (param.summary);
spawn_env += "SWAYNC_BODY=%s".printf (param.body);
spawn_env += "SWAYNC_URGENCY=%s".printf (param.urgency.to_string ());
spawn_env += "SWAYNC_CATEGORY=%s".printf (param.category);
spawn_env += "SWAYNC_ID=%s".printf (param.applied_id.to_string ());
spawn_env += "SWAYNC_REPLACES_ID=%s".printf (param.replaces_id.to_string ());
spawn_env += "SWAYNC_TIME=%s".printf (param.time.to_string ());
spawn_env += "SWAYNC_DESKTOP_ENTRY=%s".printf (param.desktop_entry ?? "");
foreach (string hint in param.hints.get_keys ()) {
if (hint.contains ("image") || hint.contains ("icon")) {
continue;
}
spawn_env += "SWAYNC_HINT_%s=%s".printf (
hint.up ().replace ("-", "_"),
param.hints[hint].print (false));
} Environment Variables being setMy question is how do I access the hint variables? HAS_NOTIFICATION_SOUND=("Discord" "Thunderbird" "vesktop")
case "$SWAYNC_URGENCY" in
Critical)
SOUNDFILE="error.oga"
;;
Normal|Low)
[[ $(swaync-client --get-dnd) == "true" ]] && exit 0
if [ -n "$SWAYNC_HINT_SOUND_FILE" ]; then
echo "$TEST"
SOUNDFILE="$SWAYNC_HINT_SOUND_FILE"
SOUNDFILE="dialog-error.oga"
elif [[ " ${HAS_NOTIFICATION_SOUND[*]} " =~ [[:space:]]${SWAYNC_APP_NAME}[[:space:]] ]] ; then
exit 0
else
SOUNDFILE="message.oga"
fi
;;
esac
mpv --really-quiet "$SOUNDFILE"
My script so far |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Noticed that this discussion is still open, the following script worked for me and plays sounds corresponding to the urgency of the notification, any hints or the application sending the notification. #!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
HAS_NOTIFICATION_SOUND=("Discord" "Thunderbird" "vesktop")
SOUNDFILE="/run/current-system/sw/share/sounds/freedesktop/stereo/suspend-error.oga"
[[ "$SWAYNC_URGENCY" != "Critical" ]] && {
# Got any sound files?
if [[ -z "${SWAYNC_HINT_SOUND_FILE-}" ]] || [[ ! -f "${SWAYNC_HINT_SOUND_FILE//\'}" ]]; then
SOUNDFILE="/run/current-system/sw/share/sounds/freedesktop/stereo/message.oga"
else
SOUNDFILE="${SWAYNC_HINT_SOUND_FILE//\'}"
fi
# Are we busy?
[[ $(swaync-client --get-dnd) == "true" ]] && exit 0
# Has the program its own sound effect?
# Do this https://unix.stackexchange.com/questions/251013/bash-regex-capture-group
SWAYNC_APP_NAME=${SWAYNC_APP_NAME:-"No name"}
[[ " ${HAS_NOTIFICATION_SOUND[*]} " =~ [[:space:]]''${SWAYNC_APP_NAME}[[:space:]] ]] && {
echo "there"
exit 0
}
}
mpv --really-quiet "$SOUNDFILE" Script |
Beta Was this translation helpful? Give feedback.
Noticed that this discussion is still open, the following script worked for me and plays sounds corresponding to the urgency of the notification, any hints or the application sending the notification.