dunst/dunstctl
2020-04-10 10:47:12 +02:00

76 lines
2.2 KiB
Bash
Executable File

#!/bin/sh
set -eu
DBUS_NAME="org.freedesktop.Notifications"
DBUS_PATH="/org/freedesktop/Notifications"
DBUS_IFAC_DUNST="org.dunstproject.cmd0"
DBUS_IFAC_PROP="org.freedesktop.DBus.Properties"
DBUS_IFAC_FDN="org.freedesktop.Notifications"
function die(){ printf "%s\n" "${1}" >&2; exit 1; }
function show_help() {
cat <<-EOH
Usage: dunstctl <command> [parameters]"
Commands:
close Close the last notification
close-all Close the all notifications
context Open context menu of latest notification
history-pop Pop one notification from history
status Check if
status-set [true|false] Set the status
help Show this help
EOH
}
function method_call() {
dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${1}"
}
function property_get() {
dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_PROP}.Get" "string:${DBUS_IFAC_DUNST}" "string:${1}"
}
function property_set() {
dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_PROP}.Set" "string:${DBUS_IFAC_DUNST}" "string:${1}" "${2}"
}
command -v dbus-send >/dev/null 2>/dev/null || \
die "Command dbus-send not found"
case "${1:-}" in
"close")
method_call "${DBUS_IFAC_DUNST}.NotificationCloseLast" >/dev/null
;;
"close-all")
method_call "${DBUS_IFAC_DUNST}.NotificationCloseAll" >/dev/null
;;
"context")
method_call "${DBUS_IFAC_DUNST}.ContextMenuCall" >/dev/null
;;
"history-pop")
method_call "${DBUS_IFAC_DUNST}.NotificationShow" >/dev/null
;;
"status")
property_get running | ( read _ _ status; printf "%s\n" "${status}"; )
;;
"status-set")
[ "${2:-}" ] \
|| die "No status parameter specified. Please give either 'true' or 'false' as running parameter."
[ "${2}" == "true" ] || [ "${2}" == "false" ] \
|| die "Please give either 'true' or 'false' as running parameter."
property_set running variant:boolean:"${2}"
;;
"help"|"--help"|"-h")
show_help
;;
"")
die "dunstctl: No command specified. Please consult the usage."
;;
*)
die "dunstctl: unrecognized command '${1:-}'. Please consult the usage."
;;
esac