1

Let's have an easy example:

I run a script which is setting a program either to mode "on" (= 1) or to mode "off" (= 0). The script is getting actuated by a simple click on the regarding .desktop-file.


The script looks like that:

#!/bin/bash

DESKT=/home/user/program.desktop

if [ $(systemctl --user status program | grep -c running) = 0 ]; then
    systemctl --user start program
    echo "Icon=program_active" >> "$DESKT"
    echo "Name=Program working" >> "$DESKT"
else
    systemctl --user stop program
    echo "Icon=Program_inactive" >> "$DESKT"
    echo "Name=Program stopped" >> "$DESKT"
fi

The .desktop-file looks like that:

[Desktop Entry]
Type=Application
Name=Program
Exec=program
Icon=program-inactive

My question now is:

If I switch on my program by actuating the .desktop-file, the name of the .desktop-file is getting changed to "Program working" what is fully correct! Unfortunately, the previous icon ("program_inactive") doesn't get changed to the now needed icon "program_active".

Instead of replacing the inactive-icon with the correct active-icon, there will be written a completely new line with "Icon=program_active" instead of replacing the existing line "Icon=program_inactive". Because of that, there are now two lines with "Icon=blabla" what is completely useless of course.

What do I have to do to replace the existing icon-line instead of writing a useless new line? Thanks a lot for your ideas!

1
  • 1
    That's an interesting idea and at the same time a blatant misuse of .desktop files. Mar 14 '17 at 12:06
4

Try overwriting the file instead.

#!/bin/bash

DESKT=/home/user/program.desktop

if [ $(systemctl --user status program | grep -c running) = 0 ]; then
    systemctl --user start program
    icon=active
    name=working
else
    systemctl --user stop program
    icon=inactive
    name=stopped
fi

cat > "$DESKT" <<EOF
[Desktop Entry]
Type=Application
Name=Program $name
Exec=program
Icon=program_$icon
EOF

Explanation:

The cat <<EOF will copy anything until the next EOF by itself, interpreting variables.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.