Check if there’s a change in the content of window (Xorg)

Solution for Check if there’s a change in the content of window (Xorg)
is Given Below:

is there any way I could check (via shell) that there’s some change in the context of a window in x11 (xorg)? like

I want to execute a certain script if there’s a change in the context of any window in current workspace ( also called as virtual desktops I think ).

Well there is xev, but that only allows you to monitor a single window. There is also xeventbind, which allows you execute a command when the resolution changes. It looks like you could modify that program to trigger on other types of events. What type of event do you want to trigger on?

Would something like this work with xev & xprop?

#!/bin/bash

set -o nounset
set -o errexit
set -o pipefail
shopt -s lastpipe

xprop -root _NET_CURRENT_DESKTOP | read -r _ _ desktop
xev -root -event property | while IFS=' ()' read -r type _ prop _; do
    case $prop in
    _NET_CURRENT_DESKTOP)
        xprop -root _NET_CURRENT_DESKTOP | read -r _ _ new_desktop
        if [[ "$desktop" != "$new_desktop" ]]; then
            printf 'Taking a screenshot of desktop (%s)!n' "$desktop"
            desktop=$new_desktop
        fi
        ;;
    esac
done