Applet to execute on/off toggle command?

Hello! I'm looking for an applet that works like a toggle button and executes an external command on events:

  1. When you click the applet, it runs a user-specified command (e.g., /usr/local/bin/my-connection-start) and changes the applet icon to "button pressed"

  2. When you click on the applet again, it runs another user-specified command (e.g., my-connection-stop) and changes the applet icon to "button released".

Is there an applet or an indicator that can do something similar to this?

Thanks for any suggestion!

2 Likes

I have minimal draft as starting to get the solution to your problem. But it works only with Ubuntu MATE 22.04 LTS.

In my solution we need to do three steps:

1.create desktop-file

mkdir -p ~/.local/share/applications
cat <<EOF | tee ~/.local/share/applications/my-toggle.desktop
#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon=mate-ax-key-no
Exec=/usr/local/bin/my-toggle
Name=my-toggle
EOF

2.create a script to modify desktop-file

cat <<\EOF | sudo tee /usr/local/bin/my-toggle
#!/bin/bash
my_toggle_desktop=~/.local/share/applications/my-toggle.desktop

if grep ^Icon=mate-ax-key-no "$my_toggle_desktop"; then
    sed "s|mate-ax-key-no|mate-ax-key-yes|g" -i "$my_toggle_desktop"
    zenity --info --text "calling /usr/local/bin/my-connection-start"
else
    sed "s|mate-ax-key-yes|mate-ax-key-no|g" -i "$my_toggle_desktop"
    zenity --info --text "calling /usr/local/bin/my-connection-stop"
fi
EOF

sudo chmod +x /usr/local/bin/my-toggle

3.navigate Caja to ~/.local/share/applications then double click on my-toogle for first time to mark it as trusted, then drag and drop it to MATE Panel.

On 22.04 LTS it works as expected

toggle

you can replace zenity by actual calls to your scripts.

But here I have a question to MATE developers - how should I adapt above method to 20.04 LTS? Icon is not updating on MATE Panel here. Maybe @vkareh or @raveit65 can help?

Note: the above method works normally with MATE 1.26 from fresh-mate PPA on 20.04 LTS.

step 2, first line should be cat <<EOF

no, it should not. We have variables inside the script, they should not be replaced by their values on script creation.

my error, sorry for the mistake.