From a bash script, is there some way to get the ID of the X window under the mouse pointer?  (edit) I need the process to be non-interactive.
How do I find the X window ID under the mouse pointer in bash?
xorg;utilities;mouse;window
xdotool exposes the pointer location (xdotool getmouselocation), and recent versions (since 2.20110530.1) indicate which window is at that location as well. None of xwininfo, wmctrl or older versions of xdotool appear to have a way to match a window by a screen position where it's visible.The underlying X library call is XQueryPointer (corresponding to a QueryPointer message). Here's a simple Python wrapper script around this call (using ctypes). Error checking largely omitted. Assumes you're using screen 0 (if you didn't know that displays could have more than one screen, ignore this).#! /usr/bin/env pythonimport sysfrom ctypes import *Xlib = CDLL(libX11.so.6)display = Xlib.XOpenDisplay(None)if display == 0: sys.exit(2)w = Xlib.XRootWindow(display, c_int(0))(root_id, child_id) = (c_uint32(), c_uint32())(root_x, root_y, win_x, win_y) = (c_int(), c_int(), c_int(), c_int())mask = c_uint()ret = Xlib.XQueryPointer(display, c_uint32(w), byref(root_id), byref(child_id),                         byref(root_x), byref(root_y),                         byref(win_x), byref(win_y), byref(mask))if ret == 0: sys.exit(1)print child_id.valueUsage example:xwininfo -tree -id $(XQueryPointer)