Как вставить постоянно изменяющуюся (динамическую) строку из текстового документа в .conkyrc как путь к изображению

683
Curtis

Я хочу изменить обложку альбома Clementine с каждой песней. По сути, я пытаюсь вставить путь изображения в Conky, который меняется каждые несколько секунд.

У меня есть скрипт, clementine.shкоторый запускается каждые 5 секунд в Конки, используя эту строку:

$ 

Скрипт запускает следующую команду:

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

Что он делает, так это находит arturlиз Clementine, обрезает его и помещает его в файл image. Строка перезаписывается каждый раз. Написанная строка выглядит так:

/tmp/clementine-art-ED9078.jpg 

Я хотел бы найти способ скопировать строку /tmp/clementine-art-ED9078.jpgиз файла imageи поместить ее в мой файл .conkyrc в строке:

$ 

Где xxxxэто, где я хочу, чтобы текст, чтобы написать, так что строка:

$ 

На самом деле будет выглядеть так:

$ 

Очевидно, что линия будет меняться с каждой песней.

Я хочу сделать это с помощью сценария (или непосредственно в Conky и т. Д.), И мне не нужно устанавливать какие-либо программы или что-либо еще для этого. Я считаю, что это «выполнимо», я просто понятия не имею, как это сделать. Я искал Интернет и не нашел ничего, что я мог бы использовать. Любая помощь, которую кто-либо может оказать мне, будет не только оценена, но и заставит мою головную боль уйти.

Примечание: я могу (из Conky) перечислить исполнителя, название и альбом, используя следующие строки в .conkyrc:

$$  $$  $$ 
1

1 ответ на вопрос

0
John1024

Using soft links

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

$ 

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/ 1' ~/.conkyrc.in >~/.conkyrc 

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

$ 

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/

    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 $

    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.

Я использовал ваш первый ответ с флагом -n, и он работал прекрасно. Спасибо Спасибо спасибо! Curtis 9 лет назад 0