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

  1. if [ "$SENDER" = "volume_change" ]: This conditional checks that the script was triggered by the volume_change event.
  2. VOLUME="$INFO": For the volume_change event, SketchyBar places the new volume percentage (e.g., "75") into the $INFO environment variable. The script stores this in a local VOLUME variable.
  3. case "$VOLUME" in ...: A case 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).
  4. sketchybar --set "$NAME" ...: Finally, the script updates the item's icon with the selected symbol and its label with the volume percentage.