3

I want to create a shortcut key that toggles Airplane Mode On/Off, but do not know what command to use.

To create a shortcut I go to Settings> Keyboard> Shortcuts and then specify a command to be run whenever a given accelerator (shortcut key) is activated.

What command should be specified here to toggle Airplane Mode via a shortcut key?

Note that a while ago I asked a similar question, but the answer that I got back then can not be applied here, since a shortcut key command can not change between "on" and "off", but should truly toggle the Airplane Mode not just activate or deactivate it.

lindhe
  • 682

2 Answers2

3

You can use the following simple bash script:

#!/bin/bash

wifi="$(nmcli nm wifi | awk 'FNR == 2 {print $1}')"

if [ "$wifi" = "enabled" ]; then
    nmcli nm wifi off
else
    nmcli nm wifi on
fi

Don't forget to make it executable:

chmod +x /path/to/script

Then add your custom shortcut to this script (it's up to you what shortcut want to choose - it should be something simple for you, like Alt+F5):

add custom shortcut

Radu Rădeanu
  • 169,590
  • Oh, it's you again. Hi! :) I can't seem to get this working. I did make a new file ~/scripts/unicornsandrainbows.sh where I copy-pasted all your above code and made it executable, but when I run ~/scripts/unicornsandrainbows.sh in a terminal (just to know whether or not it was working) nothing seems to happen. Any ideas? – lindhe Nov 01 '13 at 20:33
  • @Lindhe94 I updated the script in the time... Maybe you copied first version which had a problem. – Radu Rădeanu Nov 01 '13 at 20:35
  • That was totally the case. This is great - you just give the answers to all my answers. Think I'll keep an eye open for your responses from now on. ;) – lindhe Nov 01 '13 at 20:40
  • 1
    This is close, but does not actually toggle the airplane mode, but instead just enable/disables wifi. – kzh Dec 28 '14 at 17:17
  • Error: argument 'nm' not understood. Try passing --help instead. – mLstudent33 Jan 12 '21 at 02:16
  • I used your script, and in the command part of the set custom shortcut menu, I typed the path to my bash exec. I can execute this exec in my terminal, but it does not work with the shortcut. Any idea why? – Alexandre Laborie Nov 17 '22 at 04:24
1

Use nmcli radio wifi as nmcli nm is outdated.

Here is the updated script.

    #!/bin/bash
wifi="$(nmcli radio wifi)"

if [ "$wifi" = "enabled" ]; then
    nmcli radio wifi off

else
    nmcli radio wifi on

fi