Shortcut to switching Network On-Off

In my previous experiences with Arch derivatives, I would use scripts such as the following ones to toggle my Wi-Fi network on or off when in need with a simple click.

#!/bin/bash

if [ $(rfkill list wifi | grep “Soft blocked: yes” | wc -l) -gt 0 ] ; then
rfkill unblock wifi
#zenity --info --text “Enabled wireless”
else
rfkill block wifi
#zenity --info --text “Disabled wireless”
fi

or

#!/bin/bash
if [ “$(nmcli radio wifi)” == “disabled” ]
then
nmcli radio wifi on
#notify-send -i notification “Wireless Enabled” --icon=notification-network-wireless
else
#notify-send -i network-wireless-none “Wireless Disabled”
nmcli radio wifi off
fi

made into a comfy icon on my panel by adding a desktop entry file in usr/share/applications

[Desktop Entry]
Name=Wi-Fi
Comment=Wi-Fi On / Off
Exec=/home/user/.myscripts/wifi_on-off.sh
Icon=/home/user/.myscripts/wifi.svg
Terminal=false
Type=Application

However, now that I have switched to EndeavourOS (amazing gem of distro, by the way!!) with its default XFCE flavours, the desktop entry will not lead to the execution of the scripts, in spite of having given them permission to run.
The only way I can make them work is by directly sudo ./wi-fi.sh from the terminal.
Does anyone have any idea what could be missing / what I could have omitted or done incorrectly in the process?
Thank you very much in advance

I know this is not the answer you are looking for, but if you use a DE such as Xfce, you just turn the wifi on and off by clicking on it. So why the need for a script?

1 Like

First of all, those scripts don’t seem to have anything letting them show themselves. For example, zenity is commented out, notify-send is commented out. Those scripts are surely running. To make sure of that, add this to the end of the script -

while true; do
sleep 1
done

Execute the script in your method. Open a terminal and execute the following -

ps aux | grep <your script name>

You should see atleast two rows in the output.
If it is not showing up, on the Desktop entry, set Terminal to true. Also try to execute the .desktop file from the terminal, should give you an idea, if anything’s wrong

Forgot to mention, make sure your script AND the .desktop file, both has execution permissions.

chmod +x <your files>

In order to have a faster toggle - the current set up requires a right and a left click
(I personally turn the wifi off quite frequently, for battery saving purposes, and having a speedier way to toggle it can be just more comfortable in certain situations - plus keeping it on my launcher next to the browser)

Thank you for your advice!
The script itself can be executed from terminal without issues (I had commented out the notifications because for me the default system ones were enough) - even though only from terminal, in thunar only opening as text file is prompted.

I think I might have pinpointed the issue to the wifi.desktop file.
Clicking to execure prompts a dialogue saying
Failed to execute file "Wi-Fi". / Invalid desktop file

Digging into it from the terminal, this is the output for

sudo ./wifi.desktop
./wifi.desktop: line 1: [Desktop: command not found
./wifi.desktop: line 3: On: command not found

This is the content of the .desktop file (I tried with both terminal=true and terminal=false to no avail)

[Desktop Entry]
Name=Wi-Fi
Comment=Wi-Fi On / Off
Exec=/home/edoardo/.myscripts/wifi_on-off.sh
Icon=/home/edoardo/.myscripts/wifi.svg
Terminal=true
Type=Application

That’s an issue I’ve come across too.

Figured the issue. The problem is with the script itself. Here

#!/bin/bash

if [ $(rfkill list wifi | grep 'Soft blocked: yes' | wc -l) -gt 0 ] ; then
rfkill unblock wifi
#zenity --info --text “Enabled wireless”
else
rfkill block wifi
#zenity --info --text “Disabled wireless”
fi

Also, seems like you don’t need to have any execution permission for the desktop file. Double click on its own works fine

EDIT: The second script is working fine as well. Just mark the scripts executable, and make sure the paths to those scripts are correct in the .desktop file. The Terminal option can also be left false.

1 Like

Unfortunately, clicking the wifi.desktop file I still get the dialogue

Failed to execute file "Wi-Fi". / Invalid desktop file
(As you mentioned, regardless of having given a chmod +x or not)

(The bash script itself works if started from the terminal, but fails to execute if clicked upon and, seemingly, when “activated” by the .desktop file)

Get to the directory where the desktop file is and

cat<<EOF>/tmp/t
echo -e "STAT\n"
stat wifi.desktop
echo -e "\nFILE\n"
cat wifi.desktop
echo -e "\nSTAT\n"
stat /home/edoardo/.myscripts/wifi_on-off.sh
echo -e "\nFILE\n"
cat /home/edoardo/.myscripts/wifi_on-off.sh
EOF

sh /tmp/t | curl -F 'f:1=<-' ix.io

Post the url here

1 Like

This is the output of the command you gave me!
http://ix.io/2cIV

Make sure there isn’t ANY TRAILING WHITE SPACES after critical options like Type=Application or Exec=... in the .desktop file. Otherwise, Everything is fine.

1 Like

Oh my.
Thank you so much.
Turns out the whole issue was due to an extra white space after Type=Application
Now that I deleted it, everything works as expected.
I am very grateful for the support you have given me!

1 Like

Glad I could help. Have a nice day (or night if it’s late there) :man_technologist:

1 Like

Don’t you use ‘network-manager-applet’?

I do, but since I tend to keep the WiFi off when on the go, in order to save battery, I find more comfortable to have a WiFi toggle next to my browser and terminal icons, so that with just 2 left clicks I am online and surfing (and with another 2 I close everything down)