Plugin: volume.sh
This plugin displays the current system audio volume percentage and an icon that changes based on the volume level.
Usage
This plugin is designed to be used with an item that subscribes to the volume_change
event. This is very efficient, as the script only runs when the volume is actually adjusted.
sketchybar --add item volume right \
--set volume script="$PLUGIN_DIR/volume.sh" \
--subscribe volume volume_change
Source Code
#!/bin/sh
# The volume_change event supplies a $INFO variable in which the current volume
# percentage is passed to the script.
if [ "$SENDER" = "volume_change" ]; then
VOLUME="$INFO"
case "$VOLUME" in
[6-9][0-9]|100) ICON=""
;;
[3-5][0-9]) ICON=""
;;
[1-9]|[1-2][0-9]) ICON=""
;;
*) ICON=""
esac
sketchybar --set "$NAME" icon="$ICON" label="$VOLUME%"
fi
Explanation
if [ "$SENDER" = "volume_change" ]
: This conditional checks that the script was triggered by thevolume_change
event.VOLUME="$INFO"
: For thevolume_change
event, SketchyBar places the new volume percentage (e.g., "75") into the$INFO
environment variable. The script stores this in a localVOLUME
variable.case "$VOLUME" in ...
: Acase
statement is used to select an appropriate volume icon based on the percentage.[6-9][0-9]|100
: Matches volumes from 60-100 (high volume icon).[3-5][0-9]
: Matches volumes from 30-59 (medium volume icon).[1-9]|[1-2][0-9]
: Matches volumes from 1-29 (low volume icon).*
: Matches anything else, including 0 (mute icon).
sketchybar --set "$NAME" ...
: Finally, the script updates the item'sicon
with the selected symbol and itslabel
with the volume percentage.