Retrieve X11 window ID of a just-launched GUI-program

processscriptingwindowwmctrlx11

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?

Best Answer

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).

Related Question