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
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.