Shell – How to close all applications in a workspace

fedoragnomegnome-shellgnome3workspaces

Is there a gnome 3 extension or a fedora 25 shortcut to close all opened applications in a workspace? (Closing the workspace itself by pressing an X for example).

I've seen that functionality somewhere long time ago, forgot where, but it's not available in fedora by default.

Best Answer

The answer below was written and tested on Gnome3 / Ubuntu. Please mention if you run into issues when running it on Fedora / Gnome.


Script to close all application windows on a specific workspace in Gnome

The script below will close all windows on a specific workspace, with two options:

  1. When run without arguments, it closes all windows on the current workspace, e.g.:

    python3 /path/to/script.py
    
  2. when run with a specific workspace as argument, it will close all windows on that workspace, e.g.:

    python3 /path/to/script.py 0 
    

    will close all applications on workspace 1. Note that the first workspace has index 0

The script

#!/usr/bin/env python3
import subprocess
import sys

arg = sys.argv[1:]

def check_close(w_id):
    w_data = get(["xprop", "-id", w_id])
    if "_NET_WM_WINDOW_TYPE_NORMAL" in w_data:
        subprocess.call(["wmctrl", "-ic", w])

def get(cmd):
    return subprocess.check_output(cmd).decode("utf-8")

wlist = [l.split() for l in get(["wmctrl", "-lG"]).splitlines()]

arg = arg[0] if arg else [
    l.split()[0] for l in get(["wmctrl", "-d"]).splitlines() if '*' in l
    ][0]

for w in [w[0] for w in wlist if w[1] == arg]:
    check_close(w)

How to use

  1. The script needs wmctrl, which should be installed if it isn't on your system.
  2. Copy the script into an empty file, save it as close_wins.py
  3. Now test- rune the script from a terminal window, with the targeted workspace as argument, e.g.:

    python3 /path/to/close_wins.py 2
    

    to gracefully close all windows on workspace 3 (0 = worspace 1)

    To close all windows on the current workspace, run it without arguments:

    python3 /path/to/close_wins.py
    
  4. If all works fine, add it to a shortcut key: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/close_wins.py
    

...or run it in any other way you'd prefer.

Explanation

The command wmctrl -lG will give us information on the currently opened windows. The output looks like:

0x018000f9  0 135  206  650  500  jacob-System-Product-Name pscript_6.py (1,6 GB Volume /media/jacob/6C08-F637) - gedit
0x01600a09  1 283  275  724  443  jacob-System-Product-Name jacob@jacob-System-Product-Name: ~
0x018018be  1 152  407  650  500  jacob-System-Product-Name Untitled Document 1 - gedit
0x0140008c  2 0    101  1280 960  jacob-System-Product-Name gnome - Can I have a countdown window show the time until next suspend? - Ask Ubuntu - Mozilla Firefox
0x01600a49  2 47   146  724  443  jacob-System-Product-Name jacob@jacob-System-Product-Name: ~

From the second column, we can retrieve the window's location:

0x0140008c  2 0    101  1280 960  jacob-System-Product-Name gnome - Can I have a countdown window show the time until next suspend? - Ask Ubuntu - Mozilla Firefox

The 2 means the window is on workspace 3, since 0 refers to the first workspace.

If the script runs with the workspace as argument, the script parses out the corresponding window- id's and subsequently closes them gracefully with the command (e.g.):

wmctrl -ic 0x0140008c

If the script runs without the workspace as argument, the script retrieves the current workspace from the command:

wmctrl -d

and subsequently uses the current workspace internally as argument.

Related Question