7

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.

3

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.

  • Note that this will not work in a Wayland session. – mattdm Mar 8 '17 at 12:16
  • @mattdm Not sure what OP is using, but isn't gnome on X? – Jacob Vlijm Mar 8 '17 at 12:26
  • In Fedora 25, the default GNOME session is on Wayland with X as a fallback. – mattdm Mar 8 '17 at 12:41
  • 1
    @JacobVlijm no no it works great! the current workspace is 0 not 1, the second is 1, so if I want to close the second workspace, I do script.py 1 I just noticed that after reading the output of wmctrl -lG – Lynob Mar 8 '17 at 14:10
  • 1
    @DanielAlder the language is python, not so much Perl :) – Jacob Vlijm Apr 4 '17 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.