How do I insert a constantly changing (dynamic) string from a text document into .conkyrc as an image path

2

I want to change the Clementine album art with each song. Essentially, I am trying to insert an image path in Conky that changes every few seconds.

I have a script clementine.sh that runs every 5 seconds in Conky using this line:

${execi 5 sh ~/.config/openbox/conky/clementine.sh}

The script runs the following command:

qdbus org.mpris.clementine /Player org.freedesktop.MediaPlayer.GetMetadata | grep arturl | cut -c16- > ~/.config/openbox/conky/image

What this does, is it finds the arturl from Clementine, trims it, and places it in the file image. The line overwrites each time. The line that is written looks like this:

/tmp/clementine-art-ED9078.jpg

What I would like to do, is find a way to copy the line /tmp/clementine-art-ED9078.jpg from the file image and place it in my .conkyrc file in the line:

${image "xxxx" -p 0,620 -s 75x75}

Where the xxxx is, is where I want the text to write to, so that the line:

${image "xxxx" -p 0,620 -s 75x75}

Will actually look like:

${image /tmp/clementine-art-ED9078.jpg -p 0,620 -s 75x75}

Obviously, the line will change with each song.

I am wanting to do this with a script (or directly in Conky, etc.), and not have to install any programs or anything to accomplish this. I believe it is "do-able", I just don't have a clue how to do it. I have scoured the Internet and came up with nothing I can use. Any help anyone can give me will not only be appreciated, it will make my headache go away.

Note: I am able (from Conky) to list artist, title, and album using the following lines in .conkyrc:

${font sans-serif:normal:size=8}${execi 5 qdbus org.mpris.clementine /Player org.freedesktop.MediaPlayer.GetMetadata | grep artist}

${font sans-serif:normal:size=8}${execi 5 qdbus org.mpris.clementine /Player org.freedesktop.MediaPlayer.GetMetadata | grep title}

${font sans-serif:normal:size=8}${execi 5 qdbus org.mpris.clementine /Player org.freedesktop.MediaPlayer.GetMetadata | grep album}

Curtis

Posted 2014-10-17T04:41:41.087

Reputation: 33

Answers

1

Using soft links

In your ~/.conkyrc file, add the line:

${image /tmp/conky.jpg -p 0,620 -s 75x75}

After the qdbus line in your script, add the line:

ln -sf "$(cat ~/.config/openbox/conky/image)" /tmp/conky.jpg

This ln command updates the link file /tmp/conky.jpg to point to whatever image file name is in the ~/.config/openbox/conky/image file.

To get the behavior that you want, you may want to experiment with two flags on the conky image line: -n tells conky not to cache the image while -f interval to specifies conky's cache flush interval for that image.

Using awk

To summarize, the name of your image file is in ~/.config/openbox/conky/image and you want to transfer it to your ~/.conkyrc file. So, after the qdbus line in your script, add the line:

awk -v "img=$(cat ~/.config/openbox/conky/image)" '/image/{sub("xxxx", img)} 1' ~/.conkyrc.in >~/.conkyrc

where ~/.conkyrc.in is your prototype conkyrc file, the one that contains the line:

${image xxxx -p 0,620 -s 75x75}

The awk command replaces the xxxx with the name of your image file. After the awk command is run, a new ~/.conkyrc file is created with the correct image name.

How it works:

  • -v "img=$(cat ~/.config/openbox/conky/image)"

    This creates an awk variable img and assigns to it the contents of ~/.config/openbox/conky/image.

  • /image/{sub("xxxx", img)}

    This looks for lines containing the word image. For any line containing that word, the first occurrence of xxxx is replaced with the value of variable img.

  • 1

    This cryptic command just tells awk to print every line it receives from the input file.

  • ~/.conkyrc.in

    This is input file, the one containing the dummy ${image xxxx .... line.

  • >~/.conkyrc

    This tells the shell to redirect the output of awk to the your conkyrc file, overwriting the previous copy.

Using m4 (Advanced)

Create the file ~/.conkyrc.m4 which looks just like your normal conky file except that it contains the line:

${image translit(include(/home/user/.config/openbox/conky/image), [
])) -p 0,620 -s 75x75}

Then, run:

m4 ~/.conkyrc.m4 >~/.conkyrc

m4 will recognize translit and include as commands that it should obey. When it sees the include command, it will read your ~/.config/openbox/conky/image and include the contents. That file will likely have a trailing new line character. The translit command is there to remove that newline.

m4 is a very powerful macro language that you can use to manage many parts of your conkyrc file. If you don't want to take the time to learn a new language, it is probably simpler to stick to the awk solution.

John1024

Posted 2014-10-17T04:41:41.087

Reputation: 13 893

I used your first answer with the -n flag and it worked beautifully. Thank you, thank you, thank you! – Curtis – 2014-10-17T14:56:51.173

I used the above solution (soft link) like this:

  1. make the soft link with:

{execi 15 ln -sf "$(cat path_to_text_file_with_image_path.txt)" /tmp/conky.jpg} and after that 2) ${image /tmp/conky.jpg -p 0,620 -s 75x75} – Tina – 2019-03-06T11:35:45.447