You are not logged in.

#1 2022-06-24 08:15:47

geekmiki
Member
Registered: 2011-10-10
Posts: 72

[SOLVED]Help with pactl + dunstify script

Hi,

I am trying to adapt a script that changes volume and notifies with dunstify.
The goal is to have a function that gets the sink name and based on conditions converts it to something shorter, i.e. "alsa_output.pci-0000_06_00.6.analog-stereo" -> "laptop speakers", and then uses this rewritten output in dunstify as the appname parameter.

#!/bin/bash
function get_sink() {
sink=$(pactl get-default-sink)
if [[ $stdout == 'alsa_output.pci-0000_06_00.6.analog-stereo' ]]
then
echo Speakers
fi
}
function send_notification() {
volume=$(pamixer --get-volume)
dunstify -a "$stdout" -u low -r "9993" -h int:value:"$volume" -i "volume-$1" "Volume ${volume}%" -t 2000}

Any help appreciated to make this work.
Thanks!

Last edited by geekmiki (2022-06-24 14:22:45)

Offline

#2 2022-06-24 11:24:53

geekmiki
Member
Registered: 2011-10-10
Posts: 72

Re: [SOLVED]Help with pactl + dunstify script

Solved my own problem.
Here's the script if someone ever finds it useful:

!/bin/bash
function get_volume() {
        pamixer --get-volume
}
function get_icon() {
        if [[ "$1" -lt 34 ]]
    then
        echo -n "audio-volume-low-symbolic"
    elif [[ "$1" -lt 67 ]]
    then
        echo -n "audio-volume-medium-symbolic"
    elif [[ "$1" -le 100 ]]
    then
        echo -n "audio-volume-high-symbolic"
    fi
}
function get_sink() {
        if [[ $(pactl get-default-sink) == 'alsa_output.usb-FIIO_FiiO_USB_DAC-E10-01.analog-stereo' ]];
     then
        echo "Headphones"
     elif [[ $(pactl get-default-sink) == 'alsa_output.pci-0000_06_00.6.analog-stereo' ]];
     then echo "Laptop"
     elif [[ $(pactl get-default-sink) == 'alsa_output.pci-0000_01_00.1.hdmi-stereo' ]];
     then echo "Speakers"
     else
        echo "Other sink"
fi
}
function send_notification() {
        volume=`get_volume`
        vol_icon=`get_icon $volume`
        get_sink=`get_sink`
        dunstify -a "Volume" -u low -r "9993" -h int:value:"$volume" -i "$vol_icon" "$get_sink" "$volume%" -t 2000
}
case $1 in
up)
        # Set the volume on (if it was muted)
        pamixer -u
        pamixer -i 5 --allow-boost
        send_notification $1
        ;;
down)
        pamixer -u
        pamixer -d 5 --allow-boost   
 send_notification $1
     ;;
mute)
        pamixer -t
        if $(pamixer --get-mute); then
                dunstify -i volume-mute -a "Volume" -t 2000 -r 9993 -u low "Muted"
        else
                send_notification up
        fi
        ;;
esac

Last edited by geekmiki (2022-06-24 11:25:42)

Offline

#3 2022-06-24 12:00:04

2ManyDogs
Forum Moderator
Registered: 2012-01-15
Posts: 4,645

Re: [SOLVED]Help with pactl + dunstify script

Please remember to mark your thread [SOLVED] (edit the title of your first post).


How to post. A sincere effort to use modest and proper language and grammar is a sign of respect toward the community.

Offline

#4 2022-06-24 13:13:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED]Help with pactl + dunstify script

I'm pretty sure the following would do the same thing (I didn't do much to the dunstify commands as I've never used that tool):

#!/bin/sh

case $1 in
	up)   pamixer -u; pamixer -i 5 --allow-boost ;;
	down) pamixer -u; pamixer -d 5 --allow-boost ;;
	mute) pamixer -t ;;
esac

volume=$(pamixer --get-volume)
case $volume in
	0-34)   icon=audio-volume-low-symbolic ;;
	35-67)  icon=audio-volume-medium-symbolic ;;
	68-100) icon=audio-volume-high-symbolic ;;
esac

case $(pactl get-default-sink) in
	'alsa_output.usb-FIIO_FiiO_USB_DAC-E10-01.analog-stereo')  sink=Headphones ;;
	'alsa_output.pci-0000_06_00.6.analog-stereo')              sink=Laptop ;;
	'alsa_output.pci-0000_01_00.1.hdmi-stereo')                sink=Speakers ;;
	*)                                                         sink="Other sink" ;;
esac

if $(pamixer --get-mute); then
	dunstify -i volume-mute -a "Volume" -t 2000 -r 9993 -u low "Muted"
else
	dunstify -a "Volume" -u low -r "9993" -h int:value:$volume -i $icon "$sink" "$volume%" -t 2000
fi

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2022-06-24 14:23:26

geekmiki
Member
Registered: 2011-10-10
Posts: 72

Re: [SOLVED]Help with pactl + dunstify script

Thanks @Trilby, much cleaner than mine.

Offline

Board footer

Powered by FluxBB