I want to retrieve the X id of GUI programs I launch in background, in order to work on their windows properties. I've been so far using this workaround:

myprogram &
sleep 1
winID=$(wmctrl -l | awk '/./{line=$0} END{print $1;}')

But this relies on three heavy assumptions:

  • the program will take less than 1 second to get its window opened (not to mention than 1 second can be far too long)
  • the window will be appened at the end of the wmctrl list
  • no other window will be opened meanwhile

Is there any signal myprogram will send once it has opened a window? How can I intercept it?

2 Answers 2


To get the Window ID in my program, I have the program set the title to something unique, then have the program start wmctrl and parse its output (and not the shell script that started the program), and then report on the Window ID (most often via a file).

Since the program doesn't continue until the windows are open, you will never have to wait to long.

This of course requires that you can change myprogram ( ie. compile from source).

  • This is still a workaround, isn't it? ;) (and rather heavy) I feel like there is something standard because Gnome, for example, kindly warns you when an application has been opened in background with a message like "** is ready". Is there an event dispatched any time a window opens? How to intercept it?
    – iago-lito
    Dec 17, 2014 at 18:34
  • @Iago-lito Not sure what you call a workaround, you probably can ask X (via gnome) to get you the ID, but not AFAIK from outside the program. Parsing the output of wmctrl is just simpler. I don't know if there is a gnome event that you can catch, but by the time I check for the window title that I just set it is always there (or up to now it has always been there).
    – Anthon
    Dec 17, 2014 at 18:48
  • Okay, with "workaround" I meant this didn't sound so straightforward, but from what you say it seems like this depends much on the program itself. Let's go on with parsing the output of wmctrl -l then. Thank you by the way :) I'll still wait one day or so just in case someone has new insights into this. Edit: argh! `can't vote you up yet.
    – iago-lito
    Dec 17, 2014 at 18:59
  • In parsing the output of wmctrl, take care as there are porgrams that have titles without text etc. For my python based programs I use ruamel.ewmh (shameless plug for my own stuff).
    – Anthon
    Dec 17, 2014 at 19:18
  • 3
    Gnome can do that because it's the window manager. The WM has special status in X, it gets notified of all window-related events. I don't think there's a way for other programs to get notified similarly.
    – Barmar
    Dec 17, 2014 at 20:04

Using python gi library, (assuming that this program that has been launched most recently is on top of the window stack)

import gi
gi.require_version('Wnck', '3.0')
from gi.repository import Wnck , Gtk

while Gtk.events_pending() :



PS. Not tested

You must log in to answer this question.

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