------------------------------------------------------------------------------- X Window IDs The X window ID is the number by which the X window system (the program that displays application windows) references windows, and other objects on the display. Every window, and even every sub-window (scroll panes, title bars, buttons, input boxes, etc etc etc ) has such an ID. The xwininfo allows you to list these things, and title (application) names that are assigned, their size and relative positions within each other, and how that equates to an absolute position on screen. On special window is the 'root' window, which is the whole screen. WARNING. if you grab a window, and another window is over the top of it the image contents will contain the overlaping window!!! So make sure when grabbing windows that window is ON TOP. Window Control (vital for handling windows via scripts) xdotool General perpose X Window control tool xwit Basic window moving/resizing focus (can move move too) xwininfo window information (title, size, location) xkill basic window kill xdpyinfo Display Information xprop Get window/display property information wmctrl -l List all the application windows (add -G for icon/position/size) xlsclients -l Faster list clients ------------------------------------------------------------------------------- XTerm and Gnome Terminal Windows Xterms, and many other terminal windows report the window ID in the environment variable "WINDOWID" as a decimal number. So scripts can easily grab the contents of the terminal window from which it was run. For example try this, which asks ImageMagick to display the contents of the terminal you ran the command from... display x:$WINDOWID However for drawing to a XTerm window we need the child window to grab, modify and replace, otherwise it does not work. But if no children are found then we are using a gnome terminal window, and you just use $WINDOWID directly. window=`xwininfo -children -id $WINDOWID |\ sed -n 's/^ *\(0x[^ ]*\).*/\1/p'`; \ window="${window:-$WINDOWID}"; \ convert x:$window -background black \ -draw 'fill black rectangle 40,40 160,160' \ -draw 'stroke red line 50,50 50,150 line 50,150 150,150' \ -draw 'fill lime circle 110,100 80,100' \ -draw 'stroke dodgerblue line 50,150 150,50' \ png:- |\ display -window $window - Here we use xloadimage to set the 'background' image of the window. window=`xwininfo -children -id $WINDOWID |\ sed -n 's/^ *\(0x[^ ]*\).*/\1/p'`; \ window="${window:-$WINDOWID}"; \ convert rose: rose.png xloadimage rose.png -windowid $window These methods of directly drawing into another applications window is actually used a lot, For example the "ghostscript" program is often called to 'draw' postscript into a particular application sub-window, of a postscript viewer, such as "gv", "gsview" It is also how a separate "acroread" program can display a downloaded PDF document directly in a "firefox" Web browser! ------------------------------------------------------------------------------- Get Current Active Window ID To get the currently active window look for the appropriate Properity setting on the root window. Typicaly returned in '0x' hexidecimal or as a decimal number. The "obxprop" is provided by "openbox" window manager is UTF-8 string complient. Current Window info... xprop -root _NET_ACTIVE_WINDOW | sed 's/.* //' obxprop --root _NET_ACTIVE_WINDOW | sed 's/.* //' # shell variables of id (decimal), location, size (no title or class) xdotool getwindowfocus getwindowgeometry -shell # summery of height width title xwit -print | sed 's/.*: //; s/'\''/# '\''/;' # shell variables of id (hexidecimal), location, size, title xwit -print | \ sed 's/://; s/^/id=/; s/'\''/\x01/g; s/\x01/title='\''/; s/\x01$/'\''/; s/\x01/'\'\\\\\'\''/g; ' Iconify Current Window... xwit -iconify xdotool getwindowfocus windowminimize However the reported ID is generally wrong for GTK apps (like Firefox) and the windows immediate parent is actually needed... winid=`xdpyinfo | sed -ne 's/^focus:.*\(0x[^,]\+\).*/\1/p'` if xwininfo -id $winid | grep -q "(has no name)"; then winid=`xwininfo -children -id $winid | awk '/^ *Parent/ {print $4}'` fi NOTE: Typically for GTK the parent window is 1 less than the focus window as such decrementing by one is faster, that asking the X server. But there is no gurantee that the ID is one less. winid=`printf "0x%x\n" $(( $winid - 1 ))` # Parent = one less To also get the windows title # Get the current windows information wininfo=`xwit -print -current` winid=`echo "$wininfo" | sed 's/:.*//'` wintitle=`echo "$wininfo" | sed "s/^[^']*'//; s/'\$//"` or if you have the Windows ID... wintitle=`xprop -id $winid WM_NAME | sed 's/^[^"]*"//; s/"$//'` ------------------------------------------------------------------------------- Window Information... What window needs attention xprop -root _NET_WM_STATE_DEMANDS_ATTENTION | sed 's/^.*: *//' Borders of a window xprop _NET_FRAME_EXTENTS | sed 's/^.*= *//' 2, 2, 26, 7 Desktops window is on Information provided by the window manager (can not be set) Openbox sets it to 4294967295 to mean migrate to all desktops) xprop _NET_WM_DESKTOP | sed 's/^.*= *//' 0 The coordinates of the active area of the window. This excludes the window decoration, making it suitible video grabbing unset x y w h eval $(xwininfo -id $(xdotool getactivewindow) | sed -n -e 's/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p;' \ -e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \ -e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \ -e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" ) echo -n "$x $y $w $h" ------------------------------------------------------------------------------- Search for Specific Window First you should keep in mind that you generally find many matches for a specific application. The one you want is probably (but not always) the first one matched. For specific window ( -name "Name" )... xwininfo -root -all | grep '"Name"' xwininfo -root -tree | awk '/"mutt"/ {print; exit}' xdotool search -limit 1 --classname Name getwindowgeometry -shell xwit -names Name -print xlsclients | grep " -name mutt " # no window id (see below) See next section on "Search Speed" for more info Using xlsclients This is the fastest method and common on newer X window systems (linux) xlsclients -l However the '-l' flag that can return extra info, with lines in multi-line records requiring some special parsing. Note that the window title and current geometry is not available! xlsclients -l | perl -e ' $EOR = "Window "; # end of record (actually its the start) local $/ = "\n$EOR"; while(<>) { s/\A/$EOR/ unless $.==1; # clean up start of record s/$EOR\Z//; # clean up end of record. # Print the whole record with the record count print "--- $. ---\n$_"; # if /\/Class:.*\/Firefox$/; } print "--- Total $. Windows ---\n"; ' Other method to get: ID, Geometry and Title (for debugging) This just gets the 'active' windows. for id in $( xprop -root _NET_CLIENT_LIST_STACKING | sed 's/^.*# //; s/,//g' ) do echo id=$id xwininfo -id $id | gawk '/^xwininfo/ { id=$4; title=$0; gsub(/^[^"]+/, "", title); } /-geometry/ { geo=$2; } END { print id" "geo" "title }'; done Specific methods xwit -print -property WM_CLASS -names "claws-mail" | sed -n '1s/:.*//p' xdotool search -class FireFox getwindowgeometry %@ xdotool search -limit 1 --name 'Minecraft.*' getwindowgeometry -shell xdotool search --onlyvisible --class google-chrome getwindowgeometry -shell NOTE: "wmctrl" will not print info about a specific window though it can modify a specific window. ------------------------------------------------------------------------------- Wait for Specific Window to appear A long time ago I wrote a script to wait for a specific application window to appear (using "xwininfo" search) so I can thing do other actions to the window, such as iconify (minimise), resize, placement, desktop switch. https://antofthy.gitlab.io/software/#xwin_find It allowed me to start a program in background, wait for its window to appear, then resize/move/iconify that window. With a timeout. For Example... google-chrome & if id=`xwin_find 60 ".* - Google Chrome"`; then echo "Chrome Window Found (id=$id)" sleep 1 # pause a moment xwit -iconify -id $id else echo "ERROR: Chrome Window NOT FOUND\!" fi Here is another solution, but without a timeout! while :; do wmctrl -l | grep -q "$string" || continue : do what you need done "xdotool" is the only other tool I know of that can do this using its "search -sync" function.. firefox & xdotool search --sync --onlyvisible --class Firefox \ windowsize %@ 900 900 windowminimize %@ Unfortunatally it also does not have a timeout. Nor the ability to put the window ID on an "exec" command. ------------------------------------------------------------------------------- Wait for Specific Window to exit Again you can 'poll' for the window. However "xprop -spy" can watch a speific window, output changes that is sees happening, and exit when the window disappears! It is unknown if this is a 'poll' or a 'signal' wait. Example... xlogo & xprop -spy -id `xwin_find xlogo` ... list of all properities ... After this, you see changes to "_NET_WM_ICON_GEOMETRY" when mouse enters and leaves the window, or its icon, presumably due to changes in the icon state, made by lxpanel or openbox environment. I also see changes for "WM_STATE(WM_STATE):" switching "window state" between 'Iconic' and 'Normal'. However I do not see properity changes for anything else, such as window resizing. When the xlogo window closes, xprop exits. NOTE: If program generating the window also exits, then watching for the program exit is probably better, as it may do some extra cleanup after the window itself has exited. ------------------------------------------------------------------------------- Only one client allowed! This checks for a client, then will either raise/focus the existing client (via "wmctrl"), or launch a new client window. wmctrl -a -x 'Name' || launch_client WARNING: a window can take time to actually start properly. The launcher may need to use a temporary lock as well to ensure only one client is launched. See "flock", and example of which is in "https://antofthy.gitlab.io/info/shell/file.txt" "Exclusive Lock for a Process" Psuedo-Code... get/wait for lock test for window (now we have lock), if found, raise/unlock/exit launch client window (with appropriate class instance name) wait for client window to appear OR timeout/error/unlock/exit unlock ------------------------------------------------------------------------------- Locating special windows and information using properitiess Save the window ID of this xterm... # Save a Window ID in a properity on root window xprop -root -f THIS_XTERM 32x -set THIS_XTERM $WINDOWID # recover that Window ID from the properity jiggle_window -id `xprop -root THIS_XTERM | sed 's/.*= //'` # remove properity when no longer needed xprop -root -remove THIS_XTERM Save network port info as a string on root window.. # save String Info on root window xprop -root -f PULSE_SERVER 8s -set PULSE_SERVER tcp:otherhost:4713 # recover that Info xprop -root PULSE_SERVER PULSE_SERVER = "tcp:otherhost:4713" # remove properity when no longer needed xprop -root -remove PULSE_SERVER How can I save it as a 'WINDOW' type (rather than string or number) ------------------------------------------------------------------------------- Search Speed Using xwininfo... time xwininfo -root -tree >/dev/null real 0m0.428s About 1/2 second to do a full search of all the windows on my X window display. A search for an individual window can be speed up by aborting the search when found. time sh -c "xwininfo -root -tree | awk '/INBOX - Claws Mail/ {print; exit}'" real 0m0.195s A pretty good improvement. However it depends on just how fast the application is found as such it is an average 50% improvement in speed. The "xdotool' application does a similar search and takes about the same time. The 'early abort' has been added using the "search --limit 1" option. For example... xdotool search -limit 1 --classname mutt getwindowgeometry -shell Adding maxdepth will also speed things up xdotool search -limit 1 -maxdepth 1 --classname mutt getwindowgeometry -shell However "wmctrl" has a list option that lists the top level window manager controled windows really fast! time wmctrl -l >/dev/null real 0m0.027s This is more than an order of magnitude faster than "xwininfo", as it it only looking at the top level children of the root window. It only lists WindowID, sticky, client and title information but other options can include location/size information of the appl window. Also it only shows 'visible' windows. I believe it is implementing a depth search limit. In my case it would probably be a depth search limit of 2, and only for window manager windows. This seems to be based on a core X utility "xlsclients" which is even faster. time xlsclients -l >/dev/null real 0m0.008s ------------------------------------------------------------------------------- SPECIAL NOTES: xwit is an old application that seems to go wrong in certain situations xwit -current -print Fails to return anything for Firefox Navagator Windows! but works fine for Chrome. Reason is unknown. UPDATE - no longer works for Chrome! xwit -current -rmove x y Always moves a "xmessage -center" window downward by 9 extra pixels I generally use "xmessage" for a text display with multi-choice buttons. As such I find this behaviour annoying. -------------------------------------------------------------------------------