Skip to content

Execute on song change

Rmpc provides on_song_change property in the config file which can be used to run a command whenever the song changes. This can be used for various purposes like showing a desktop notification.

Desktop notification on song change

Assuming you have a notification daemon with support for images like dunst running. All song metadata are available to the script as environment variables. For example $TITLE, $FILE, $DURATION, etc.

  1. Create a script and place it somewhere. For example ~/.config/rmpc/notify. Below is an example of such script. Edit it to your needs or create a new one.

    ~/.config/rmpc/notify
    #!/usr/bin/env sh
    # Directory where to store temporary data
    TMP_DIR="/tmp/rmpc"
    # Ensure the directory is created
    mkdir -p "$TMP_DIR"
    # Where to temporarily store the album art received from rmpc
    ALBUM_ART_PATH="$TMP_DIR/notification_cover"
    # Path to fallback album art if no album art is found by rmpc/mpd
    # Change this to your needs
    DEFAULT_ALBUM_ART_PATH="$TMP_DIR/default_album_art.jpg"
    # Save album art of the currently playing song to a file
    if ! rmpc albumart --output "$ALBUM_ART_PATH"; then
    # Use default album art if rmpc returns non-zero exit code
    ALBUM_ART_PATH="${DEFAULT_ALBUM_ART_PATH}"
    fi
    # Send the notification
    notify-send -i "${ALBUM_ART_PATH}" "Now Playing" "$ARTIST - $TITLE"
  2. Make the script executable

    Terminal window
    chmod +x ~/.config/rmpc/notify
  3. In your rmpc’s config.ron specify on_song_change property and point it at location of the script from step 1.

    config.ron
    #![enable(implicit_some)]
    #![enable(unwrap_newtypes)]
    #![enable(unwrap_variant_newtypes)]
    (
    address: "/tmp/mpd_socket",
    on_song_change: ["~/.config/rmpc/notify"],
    ...
  4. Restart rmpc

Track song play count

This allows you to track and display song’s play count in the queue table (or header). Please note that rmpc has to be running for the script to execute and increment the play count.

  1. Configure sticker_file in your mpd.conf

  2. Create a script and place it somewhere. For example ~/.config/rmpc/playcount and make the script executable. Or append it to an existing on_song_change script.

  3. Increment playCount sticker on the song inside the script. (requires jq)

    #!/usr/bin/env sh
    sticker=$(rmpc sticker get "$FILE" "playCount" | jq -r '.value')
    if [ -z "$sticker" ]; then
    rmpc sticker set "$FILE" "playCount" "1"
    else
    rmpc sticker set "$FILE" "playCount" "$((sticker + 1))"
    fi
  4. Change your config.ron to execute the script you created on song change

    config.ron
    #![enable(implicit_some)]
    #![enable(unwrap_newtypes)]
    #![enable(unwrap_variant_newtypes)]
    (
    address: "/tmp/mpd_socket",
    on_song_change: ["~/.config/rmpc/playcount"],
    ...
  5. Change your theme to include play count sticker, for example in the queue table

    theme.ron
    #![enable(implicit_some)]
    #![enable(unwrap_newtypes)]
    #![enable(unwrap_variant_newtypes)]
    (
    song_table_format: [
    (
    ...
    ),
    (
    prop: (kind: Sticker("playCount"), default: (kind: Text("0"))),
    width: "9",
    alignment: Right,
    label: "Playcount"
    ),
    (
    ...
    ),
    ],
    ...