Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sebastiencs
Last active February 13, 2024 11:19
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sebastiencs/5d7227f388d93374cebdf72e783fbd6a to your computer and use it in GitHub Desktop.
Save sebastiencs/5d7227f388d93374cebdf72e783fbd6a to your computer and use it in GitHub Desktop.
Script to get volume notification with dunst http://imgur.com/a/qWgAw
#!/bin/bash
# You can call this script like this:
# $./volume.sh up
# $./volume.sh down
# $./volume.sh mute
function get_volume {
amixer get Master | grep '%' | head -n 1 | cut -d '[' -f 2 | cut -d '%' -f 1
}
function is_mute {
amixer get Master | grep '%' | grep -oE '[^ ]+$' | grep off > /dev/null
}
function send_notification {
volume=`get_volume`
# Make the bar with the special character ─ (it's not dash -)
# https://en.wikipedia.org/wiki/Box-drawing_character
bar=$(seq -s "─" $(($volume / 5)) | sed 's/[0-9]//g')
# Send the notification
dunstify -i audio-volume-muted-blocking -t 8 -r 2593 -u normal " $bar"
}
case $1 in
up)
# Set the volume on (if it was muted)
amixer -D pulse set Master on > /dev/null
# Up the volume (+ 5%)
amixer -D pulse sset Master 5%+ > /dev/null
send_notification
;;
down)
amixer -D pulse set Master on > /dev/null
amixer -D pulse sset Master 5%- > /dev/null
send_notification
;;
mute)
# Toggle mute
amixer -D pulse set Master 1+ toggle > /dev/null
if is_mute ; then
dunstify -i audio-volume-muted -t 8 -r 2593 -u normal "Mute"
else
send_notification
fi
;;
esac
@ahmedselim2017
Copy link

Nice work! But you can improve this via using sh not bash and also you should change the timeout of the notifications because dunstify gets timeout info in milliseconds not seconds.

@ahmedselim2017
Copy link

dunstify: command not found

You should install Dunst

@indeedwatson
Copy link

When I use this the notification window itself gets larger/smaller together with the bar, but the example in the gif doesn't. What's happening?

@manofearth
Copy link

Use -D pulse in get_volume and is_mute functions for consistency

@jtrees
Copy link

jtrees commented Jan 27, 2021

dunstify: command not found

It seems dunstify is only included with dunst by default starting with version 1.5.0:

https://github.com/dunst-project/dunst/releases/tag/v1.5.0

For instance, if you installed dunst via apt on Ubuntu 18.04, you will not have it.

@kellya
Copy link

kellya commented Jun 17, 2022

This didn't work "out of the box" for me on fedora 36. I made some modifications to get it to work, and added some customizations like choosing the bar character and added an "mtoggle" argument to toggle the microphone.

https://gist.github.com/kellya/c52c8a5dde8f530385156936a8326490

@jernejmarcic
Copy link

for whatever reason the notification disappears immediately, all other dunst notifications stay on the screen longer. (archlinux Hyprland, if it has any effect)

@ZXZhIGVsZmll
Copy link

ZXZhIGVsZmll commented May 23, 2023

for whatever reason the notification disappears immediately, all other dunst notifications stay on the screen longer. (archlinux Hyprland, if it has any effect)

Found on dunstify --help
-t, --timeout=TIMEOUT The time in milliseconds until the notification expires

Change to a larger value. So "-t 1000" will be 1 second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment