Solved: Scrolling BASH script doesn't fully work but Static PYTHON script does, anyway to mesh the two?

I found this bash script online that works partially. It will forever scroll the current song but when the song changes or pauses it keeps scrolling that same song. Any ideas guys?

#!/bin/bash
if	[ "$(playerctl status)" = "Playing" ]; then
	title=`exec playerctl metadata xesam:title`
	artist=`exec playerctl metadata xesam:artist`
	( echo "$title - $artist" | zscroll -n true -l 10 -d 0.1 ) 

elif	[ "$(playerctl status)" = "Paused" ]; then
	title=`exec playerctl metadata xesam:title`
	artist=`exec playerctl metadata xesam:artist`
	( echo "(PAUSED) $title - $artist" | zscroll -n true -l 10 -d 0.1 ) 
else
	echo ""
fi

I am currently using this python script that works but doesn’t scroll:

#!/bin/python

import sys
import dbus
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
    '-t',
    '--trunclen',
    type=int,
    metavar='trunclen'
)
parser.add_argument(
    '-f',
    '--format',
    type=str,
    metavar='custom format',
    dest='custom_format'
)
parser.add_argument(
    '-p',
    '--playpause',
    type=str,
    metavar='play-pause indicator',
    dest='play_pause'
)
parser.add_argument(
    '--font',
    type=str,
    metavar='the index of the font to use for the main label',
    dest='font'
)
parser.add_argument(
    '--playpause-font',
    type=str,
    metavar='the index of the font to use to display the playpause indicator',
    dest='play_pause_font'
)


args = parser.parse_args()

def fix_string(string):
    # corrects encoding for the python version used
    if sys.version_info.major == 3:
        return string
    else:
        return string.encode('utf-8')

# Default parameters
output = fix_string(u'{play_pause} {artist}: {song}')
trunclen = 25
play_pause = fix_string(u'\u25B6,\u23F8') # first character is play, second is paused

label_with_font = '%{{T{font}}}{label}%{{T-}}'
font = args.font
play_pause_font = args.play_pause_font

# parameters can be overwritten by args
if args.trunclen is not None:
    trunclen = args.trunclen
if args.custom_format is not None:
    output = args.custom_format
if args.play_pause is not None:
    play_pause = args.play_pause

try:
    session_bus = dbus.SessionBus()
    spotify_bus = session_bus.get_object(
        'org.mpris.MediaPlayer2.spotify',
        '/org/mpris/MediaPlayer2'
    )

    spotify_properties = dbus.Interface(
        spotify_bus,
        'org.freedesktop.DBus.Properties'
    )

    metadata = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'Metadata')
    status = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')

    # Handle play/pause label

    play_pause = play_pause.split(',')

    if status == 'Playing':
        play_pause = play_pause[0]
    elif status == 'Paused':
        play_pause = play_pause[1]
    else:
        play_pause = str()

    if play_pause_font:
        play_pause = label_with_font.format(font=play_pause_font, label=play_pause)

    # Handle main label

    artist = fix_string(metadata['xesam:artist'][0]) if metadata['xesam:artist'] else ''
    song = fix_string(metadata['xesam:title']) if metadata['xesam:title'] else ''
    album = fix_string(metadata['xesam:album']) if metadata['xesam:album'] else ''

    if not artist and not song and not album:
        print('')
    else:
        if len(song) > trunclen:
            song = song[0:trunclen]
            song += '...'
            if ('(' in song) and (')' not in song):
                song += ')'

        if font:
            artist = label_with_font.format(font=font, label=artist)
            song = label_with_font.format(font=font, label=song)
            album = label_with_font.format(font=font, label=album)

        print(output.format(artist=artist, song=song, play_pause=play_pause, album=album))

except Exception as e:
    if isinstance(e, dbus.exceptions.DBusException):
        print('')
    else:
        print(e)

The bash script uses zscroll to do the scrolling, maybe someone with experience with python can incorporate zscroll into the working Python script?

I am thinking some kind of loop to keep it checking for a change in song or end of playing

something like this(syntax not correct)

While $PID not equals empty  DO .... DONE

Someone on Reddit helped me get it running, so anyone wanting to see it or get the code its available here

Scripts for download

2 Likes

It’s good to see someone relate a positive experience on reddit. I’ve had my share of bad experiences there but to be fair, there is a good bit of useful content as well. In any case, glad to see you got it working.

What were you trying to accomplish with the script? Where does the output go?

the script basically creates a scrolling titler on polybar to display the current status/song title of whats playing on Spotify.

1 Like