2

I have a cronjob that changes my desktop background every 5 mins from a directory of flashcard images (learning a new language).

Right now the wallpaper changes when I have windows open (and cant see the desktop background). Ideally I want this script to run whenever I have closed or minimized all my windows.

This helps when there is a specific order to the flashcards and missing a few transitions spoils the experience. So when my cronjob runs I need to detect if windows/apps visible = 0 and only then change desktop background.

I looked around and found wmctrl and xlsclients but they show all kinds of info even when no apps are open or visible.

Is there some way to detect this state from the commandline so I can use it in my script?

Thanks!

2 Answers 2

1

Find out if all windows are minimized

The existing answer mentions the right tools, but has quite a few issues, and won't work as it is.

A few important ones:

  1. When using:

     s = Wnck.Screen.get_default()
    

    In a "standalone" situation (not from an existing loop), you need to add (see further below)*:

     s.force_update()
    
  2. is_minimzed() is a method, and won't work without ()

  3. The answer is not usable in another situation than OP's

A general script to just see if all windows are minimized would then be:

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

s = Wnck.Screen.get_default()
# when not run from a loop, tell it to force_update
# else there will be no output
s.force_update()

all_minimized = True

# go through all windows
for window in s.get_windows():
    # exclude windows like the desktop etc.
    if window.get_window_type() == Wnck.WindowType.NORMAL:
        # see if it is minimized
        if not window.is_minimized():
            # if any of the windows is not minimized -> all_minimized = False
            all_minimized = False


print(all_minimized)


from here:

Common Pitfalls

While the API provided by libwnck should be mostly straight-forward in general, a few pitfalls are often hit by users of the library. Explicit fetching of information At its creation, a WnckScreen object will not have fetched information from the X server. If queried immediately after its creation (via wnck_screen_get_windows() or wnck_screen_get_workspaces(), for example), the WnckScreen object will look like there are no workspaces nor windows on the screen. This information is fetched in the main event loop with an idle source, to avoid an expensive synchronous operation on startup. If no main event loop is used, or if the information is needed as soon as possible after the creation of the object, wnck_screen_force_update() can be used to explicitly fetch the information.

0

I ended up using a python script to detect this "all apps are minimized" state.

changebg = True
from gi.repository import Wnck
s = Wnck.Screen.get_default()
for window in s.get_windows():
    if window.get_window_type() == Wnck.WindowType.NORMAL:
        if not window.is_minimized:
            changebg = False

Do relevant functions based on changebg values

2
  • Did you test the script? it has a few major issues and won't work as it is. Mar 4, 2019 at 14:23
  • @JacobVlijm those corrections are awesome. Thanks for showing me how to write a good answer :) Yes I tested it. It was part of a much larger script so I just hurriedly typed out the lines from my notes that conveyed the idea. Next time will do a better job.
    – Sharun
    Mar 4, 2019 at 23:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .