10

How to know the pid of active (focused) window?

I want to write a script in which it is necessary to know whether the user is actively using a program [browsing internet with somthing say firefox] or doing something else [writing text with something say gedit]

In my case i want to download big files but don't want to hurt browsering speed. so when i browse the active window is of firefox and i want to stop downloading. When i read pdf active window is of pdf reader and i want to resume download. That's it.

1

7 Answers 7

9

Finally I managed to work get the PID of actively focused window with xdotool

xprop -id `xdotool getwindowfocus` | grep '_NET_WM_PID' | grep -oE '[[:digit:]]*$'
1
  • 1
    I take it you got xprop -id ... working?
    – Ternary
    Feb 6, 2013 at 21:10
6

Using xdotool's window stack:

xdotool getactivewindow getwindowpid

Bash:

#!/bin/bash
set -e
WINDOW_PID=$(xdotool getactivewindow getwindowpid)
...
1
  • 1
    I don't know y this isn't the accepted answer, but this is the most simple and the best one
    – Hyperx837
    Aug 11, 2021 at 12:49
4

xdpyinfo has, buried in its voluminous output, the window id of the window with the focus (see: Which window has the current focus?). With that, you can use xprop to get the the associated PID.

xprop -id `xdpyinfo | grep -Eo 'window 0x[^,]+' | cut -d" " -f2` | grep PID
6
  • 1
    Sorry, No output on 10.04
    – totti
    Jan 25, 2013 at 9:37
  • xdpyinfo | grep -Eo 'window 0x[^,]+' | cut -d" " -f2 output 0x3e00122
    – totti
    Jan 30, 2013 at 13:27
  • what does xprop -id 0x3e00122 (or whatever your new current window id is) give you?
    – Ternary
    Jan 30, 2013 at 17:40
  • 1
    It looks like some windows don't support _NET_WM_PID, but firefox and evince both seem to
    – Ternary
    Jan 30, 2013 at 17:55
  • and in this case you might find useful to take a look at xprop -id [active window] | grep WM_CLASS
    – Ternary
    Jan 30, 2013 at 18:17
1

bash

xdotool getwindowpid `xdotool getactivewindow`

fish

xdotool getwindowpid (xdotool getactivewindow)
1
  • xdotool getwindowpid $(xdotool getactivewindow) Aug 7, 2022 at 6:21
0

The following works with Gnome 43:

  1. Install https://extensions.gnome.org/extension/4974/window-calls-extended/
  2. Run gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/WindowsExt --method org.gnome.Shell.Extensions.WindowsExt.FocusPID | sed -E "s/\\('(.*)',\\)/\\1/g" to get the PID of the focussed window or use a different method.
0

I know this is very old, but Google led me here, so in case someone is searching for a xprop-only answer:

xprop -id $(xprop -root '\t$0' _NET_ACTIVE_WINDOW | cut -f2) '\t$0' _NET_WM_PID | cut -f2

NOTE: '\t$0' makes xprop use TAB as the value separator, the default delimiter for cut.

-1

You can install wmctrl then use it to list all windows, wmctrl -l.

2
  • 3
    This is a bit too brief to be helpful I think. Could you extend your answer to include some steps to take on how to get the active window for example? Or how to use the output in scripting, etc.
    – gertvdijk
    Jan 20, 2013 at 20:42
  • I'm afraid I can't help you there, I too am stuck on how to manipulate the output into something useful. Sorry. Jan 30, 2013 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .