How to Get Window Id for Xdotool Automatically

how do you get window ID for xdotool automatically

You can use awk to extract the ID from the output of wmctrl -l.

For example:

wmctrl -l | awk '/Google Chrome/ {print $1}'

xdotool will likely take that hex IDs just fine but if it can't you can convert that to the decimal representation with strtonum:

wmctrl -l | awk '/Google Chrome/ {print strtonum($1)}'

How you match just the window you want from the output in awk is up to you and your requirements.

It is probably worth noting that xdotool also appears to have a search command which takes all sorts of specifiers and patterns that you can use to get the window ID of windows you want to operate on. (It even supports a stack of matches that it supports a special format of "window ID" to operate on directly for "chained commands".)

Is there a linux command to determine the window IDs associated with a given process ID?

xwininfo and xprop permits to retrieve what you want, but it is a little tricky.

xwininfo permits to retrieve all known windows, and xprop to query X about a single window ID for your _NET_WM_PID parameter.

So far, a hacky way to do it would be:

#!/bin/sh

findpid=$1

known_windows=$(xwininfo -root -children|sed -e 's/^ *//'|grep -E "^0x"|awk '{ print $1 }')

for id in ${known_windows}
do
xp=$(xprop -id $id _NET_WM_PID)
if test $? -eq 0; then
pid=$(xprop -id $id _NET_WM_PID|cut -d'=' -f2|tr -d ' ')

if test "x${pid}" = x${findpid}
then
echo "Windows Id: $id"
fi
fi
done

Result:

mycroft:~ $ ./find_windows.sh 1919
Windows Id: 0x1800748
Windows Id: 0x181b221
Windows Id: 0x1803ad5
Windows Id: 0x181f681
Windows Id: 0x181f658
Windows Id: 0x180006d
Windows Id: 0x1800003
Windows Id: 0x1800001
Windows Id: 0x180001e

As you will see, a single process may have a certain number of known windows, even if you see only one on your screen.

Maybe you should get these tools sources in order to make what you want.

How to get an X11 Window from a Process ID?

The only way I know to do this is to traverse the tree of windows until you find what you're looking for. Traversing isn't hard (just see what xwininfo -root -tree does by looking at xwininfo.c if you need an example).


But how do you identify the window you are looking for? Some applications set a window property called _NET_WM_PID.


I believe that OpenOffice is one of the applications that sets that property (as do most Gnome apps), so you're in luck.

Automatic web-page refresh using xdotool - not sending key after window focus

After trying several different approaches to get xdotool to work correctly, I'm inclined to believe that xdotool itself is the issue. Here is what I tried, none worked.

  • Running the command (and variations - removing/adding args) from Terminal.
  • Running the command (and variations - removing/adding args) from a SH script.
  • Changing between F5 and ctrl+r keys, as they should both refresh a Firefox page.
  • Trying other parameters, such as:
    • --window to set the window the keys are to be sent to.
    • --delay to add a delay before the key is sent, after the window is focused.
    • Adding a sleep before the key is sent, after the window is focused.

I also tried these commands in a script, as the frontpage for xdotool recommends, although it states this is the "older" version, as it is separated into multiple commands. The "new" version was the version I was trying to execute before and is a single command (see question).

WID=`xdotool search "Firefox Page Title"`
xdotool windowactivate --sync $WID
xdotool key --clearmodifiers ctrl+r

All of the above attempts ALWAYS correctly focused to the window I wanted, but it does not send the key whether it was F5 or ctrl+r.

However, the following worked correctly:

xdotool selectwindow key ctrl+r

OR

xdotool selectwindow key F5

The selectwindow command, when executed, turns your cursor into a rectangular selection tool at which point you can select the window you want to be focused and, in this case, what window to send either the ctrl+r or F5 key to. Unfortunately, this is not what I was looking for, as it requires user input to work correctly.

Final Solution:

My solution (since I was attempting to use xdotool to constantly refresh a web-page) was to use the ReloadEvery Firefox add-on, which refreshes any page you set it on in any time interval you choose. It is intended to be a replica of the Opera browser's built-in automatic page refresh feature, and thus far, it works well.

For those of you who use Chrome and are looking for a similar solution, there are plenty of add-ons available for you too. https://chrome.google.com/webstore/search/auto%20refresh

What is the name of the POS1 key in xdotool?

Execute

$ xev -event keyboard

and then just type the key you want to use.

Example output:

Outer window is 0x4a00001, inner window is 0x4a00002

KeymapNotify event, serial 24, synthetic NO, window 0x0,
keys: 4294967230 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

KeyRelease event, serial 25, synthetic NO, window 0x4a00001,
root 0x1a1, subw 0x0, time 109192145, (501,285), root:(503,393),
state 0x0, keycode 36 (keysym 0xff0d, Return), same_screen YES,
" XLookupString gives 1 bytes: (0d) "
XFilterEvent returns: False

KeyPress event, serial 28, synthetic NO, window 0x4a00001,
root 0x1a1, subw 0x0, time 109193882, (501,285), root:(503,393),
state 0x0, keycode 110 (keysym 0xff50, Home), same_screen YES,
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False

KeyRelease event, serial 28, synthetic NO, window 0x4a00001,
root 0x1a1, subw 0x0, time 109193953, (501,285), root:(503,393),
state 0x0, keycode 110 (keysym 0xff50, Home), same_screen YES,
XLookupString gives 0 bytes:
XFilterEvent returns: False
^C

The interesting part is keycode 110 (keysym 0xff50, Home). So the name is Home.



Related Topics



Leave a reply



Submit