16

I need id of window which is active or focused. I try to use xdotool command. There is command:

xdotool getactivewindow

the result is saved to window stack. I want to get widnow id from this window stack. Command

xdotool getactivewindow getwindowpid

does not satisfy me. I do not want to get window id by process id.

Mariusz
  • 349
  • Side note, getactivewindow may not work in some window managers, in that case try getwindowfocus instead. The latter is documented to be "less reliable" according to xdotool documentation however, in my case (XMonad) I just need to enable EWMH with https://hackage.haskell.org/package/xmonad-contrib-0.17.0/docs/XMonad-Hooks-EwmhDesktops.html to make the former method work. – user202729 Jun 03 '23 at 11:56

3 Answers3

14

I think xdotool getactivewindow is what you want - did you try it?

It prints the window id (from the window stack) if there are no further xdotool subcommands on the command line.

In xdotool getactivewindow getwindowpid for example, getactivewindow puts the id on the window stack, and getwindowpid uses this id to query the PID. Note that running that command in the terminal will always return the ID of the terminal window, because it is active. In order to get the ID from another window try sleep 2s && xdotool getactivewindow and select the window of interest in the two seconds time span.


There is a complication when using xdotool with other tools for window handling:

While xdotool output uses decimal numbers for windwo ids, most other tools use hexadecimal numbers for output (they support both for input usually).

For example, if you find a window with xdotool getactivewindow, you will not find the result in the output of xwininfo -root -tree, that lists all windows. It needs to be converted to a hexadecimal number first:

$ xdotool getactivewindow                              
69206716
$ printf 0x%x 69206716                  
0x42002bc
$ xwininfo -root -tree | grep 0x42002bc
           0x42002bc (has no name): ("konsole" "Konsole")  1154x781+0+0  +1289+498


Converting decimal to hexadecimal:

printf 0x%x 69206716

Converting hexadecimal to decimal:

printf %i 0x42002bc
mxmlnkn
  • 556
  • 5
  • 11
Volker Siegel
  • 17,283
  • 1
    I tried xdotool getactivewindow command and problem is that it does not print result in terminal. It save result to window stack. This command prints some number but It is not window id (e.g 35651588) – Mariusz Sep 09 '14 at 11:20
  • Why do you think 35651588 is not the window id? – Volker Siegel Sep 09 '14 at 11:20
  • 1
    Yes, I think that 35651588 is not the window id. You can see windows id by xwininfo command, e.g xwininfo -root -tree - this command prints all windows and windows have another types of numbering. – Mariusz Sep 09 '14 at 11:23
  • 1
    xwininfo shows the window id as hexadecimal numbers, and xdotool shows decimal numbers, but it's just the same. – Volker Siegel Sep 09 '14 at 11:24
  • you are right. 35651588=0x2200004. xwininfo -root -tree | grep 2200004 0x2200004 "user@host:~": ("gnome-terminal" "Gnome-terminal") 1680x974+0+26 +0+51 – Mariusz Sep 09 '14 at 11:29
  • Yes, you need to convert the decimal id to a hexadecimal number. For example printf 0x%x 35651588 gives 0x2200004 - try xwininfo -root -tree | grep 0x2200004 – Volker Siegel Sep 09 '14 at 11:31
1

This command works for me

xdotool key --window "$(xdotool getactivewindow)" F5

xdotool version 3.20160805.1

1

I know this is an old question. Maybe someone will find this answer useful.

xdotool selectwindow getmouselocation --shell

then click on the window your want the id for.

AdminBee
  • 22,803
paul
  • 11