linuxbashxdotoolwmctrl

Count the minimized windows on Linux with bash


I'm trying to build a script that checks that windows under XFCE are minimized before displaying a window I've chosen (it's a part from a bigger project)

I tried to recover the count of the open windows with wmctrl but these are not the minimized windows :

CURRWORKSPACE=$(wmctrl -d | grep '*' | cut -d ' ' -f1)
OPENWINDOWS=$(wmctrl -l | cut -d ' ' -f3 | grep $CURRWORKSPACE | wc -l)

I also try with xdotool, but without success :(

I was wondering if you knew of any way to get this information. I'm on XFCE, but another way with any tool would be great

Many thanks !


Solution

  • Given a window and its id as listed by wmctrl you can use the following function to determine whether that window is minimized. Note that minimized windows are called iconic in X.

    # usage: isMinimized <windowId>
    # returns status 0 if and only if window with given id is minimized
    isMinimized() {
        xprop -id "$1" | grep -Fq 'window state: Iconic'
    }
    

    In order to count open windows you can loop over the list of window ids.

    openWindows() {
        count=0
        for id in $(wmctrl -l | cut -f1 -d' '); do
            isMinimized "$id" || ((count++))
        done
        echo $count
    }
    

    At least on my desktop environment (Cinnamon) some "windows" were always open. These windows are for instance the Desktop. I adjusted the function by filtering out these windows before looping over them. Since they were sticky and I normally don't use sticky windows I just neglected all sticky windows: $(wmctrl -l | grep -vE '^0x\w* -1' | cut -f1 -d' ').

    You can adjust the filtering to your needs. In this case all open and non-sticky windows on all workspaces/desktops are counted.