How to determine if window is maximised or minimised from bash script

I have a bash script that moves my windows from the left screen to right screen in dual-screen setup. Currently the way it works is cycling through the window ids that are given by xdotool search --onlyvisible --maxdepth 2 --class "" and then moves them to the right by the screen width. It already works... unless the window in question is maximises or minimised.

So what is needed is a way to check the current status of the window. I have found an answer that provides the way to add and remove those bits, but where is the way to check if they are set already?

If it is not possible to do via xdotool, it should be possible to reuse the window id provided by the command mentioned above.


Solution 1:

Retrieve info on the window state

You can get the info (and a lot more) from the command:

xprop -id <window_id>

To get what you are specifically looking for:

xprop -id 0x04c00010 | grep "_NET_WM_STATE(ATOM)"

The output will look like:

_NET_WM_STATE(ATOM) = _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT, _NET_WM_STATE_HIDDEN

on a window that is maximized (h + v) and minimized at the same time, or just

_NET_WM_STATE(ATOM) =

(or no output at all) if none of those is the case.

More fun

Of course, using various languages, you can use Wnck, like in the python snippet below. (snippet from window-shuffler). The snippet outputs a list, showing the window name + either True or False (minimized).

#!/usr/bin/env python3
import gi
gi.require_version('Wnck', '3.0')
from gi.repository import Wnck


def get_winlist(scr=None, selecttype=None):
    """
    get the window list. possible args: screen, select_type, in case it is
    already fetched elsewhere. select type is optional, to fetch only
    specific window types.
    """
    if not scr:
        scr = Wnck.Screen.get_default()
        scr.force_update()
    windows = scr.get_windows()
    if selecttype:
        windows = [w for w in windows if check_windowtype(w, selecttype)]
    return windows

wlist = get_winlist()
for w in wlist:
    print(w.get_name(), ",", w.is_maximized())

Output looks like:

Wnck.Window - Classes - Wnck 3.0 - Mozilla Firefox , True
Postvak IN - [email protected] - Mozilla Thunderbird , True
Showtime , False
settingsexample.vala - Visual Studio Code , False
*Niet-opgeslagen document 1 - gedit , False
desktop_weather , False
Tilix: Standaard , False

N.B.

  • xprop will handel both hex (as output from e.g. wmctrl) and decimal (as output from e.g. xdotool) id's equally e.g. either use:

    xprop -id 111371626
    

    or

    xprop -id 0x06a3656a
    
  • Methods will not work on Wayland!