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
    1
    #!/usr/bin/env sh
    2
    3
    # Directory where to store temporary data
    4
    TMP_DIR="/tmp/rmpc"
    5
    6
    # Ensure the directory is created
    7
    mkdir -p "$TMP_DIR"
    8
    9
    # Where to temporarily store the album art received from rmpc
    10
    ALBUM_ART_PATH="$TMP_DIR/notification_cover"
    11
    12
    # Path to fallback album art if no album art is found by rmpc/mpd
    13
    # Change this to your needs
    14
    DEFAULT_ALBUM_ART_PATH="$TMP_DIR/default_album_art.jpg"
    15
    16
    # Save album art of the currently playing song to a file
    17
    if ! rmpc albumart --output "$ALBUM_ART_PATH"; then
    18
    # Use default album art if rmpc returns non-zero exit code
    19
    ALBUM_ART_PATH="${DEFAULT_ALBUM_ART_PATH}"
    20
    fi
    21
    22
    # Send the notification
    23
    notify-send -i "${ALBUM_ART_PATH}" "Now Playing" "$ARTIST - $TITLE"
  2. Make the script executable

    Terminal window
    1
    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
    1
    #![enable(implicit_some)]
    2
    #![enable(unwrap_newtypes)]
    3
    #![enable(unwrap_variant_newtypes)]
    4
    (
    5
    address: "/tmp/mpd_socket",
    6
    on_song_change: ["~/.config/rmpc/notify"],
    7
    ...
  4. Restart rmpc