How can it be done using Python in Ubuntu?
3 Answers
psutil
seems to be what you might want.
What's unclear in your question is whether you want the executable name or the window title. However, if you're after the executable process's name, you can do it with psutil
and some other functions.
First, install psutil
with pip
(Python 2) or pip3
(Python 3). If the command isn't found then install the python-pip
or python3-pip
packages depending on which Python version you're working with:
# Python 2
sudo pip install psutil
# Python 3
sudo pip3 install psutil
Next, we can use psutil
in any given Python script, and pass a PID. This is an example of what it can do, by running python3
in the command line, working with the interactive Python 3 shell, and getting that process's information using Python code right on the Python shell:
>>> import psutil
>>> import os
>>> os.getpid()
6284
>>> p = psutil.Process(os.getpid())
>>> p.name()
'python3'
>>> p.exe()
'/usr/bin/python3.5'
>>> p.cmdline()
['python3']
>>>
As you can see you can get different information from the system about the process on both Windows or Linux with these utilities, though you still need to install the Python package first.
EDIT
We can also chain this with Jacob Vlijm's answer here too, to get the PID and pass it into psutil
(Note that I shorten 'subprocess' to 'sp' in my import, but it's still the subprocess
module):
import psutil
import subprocess as sp
p = psutil.Process(int(sp.check_output(["xdotool", "getactivewindow", "getwindowpid"]).decode("utf-8").strip()))
... and then continue to use psutil
to extract specific data.
Short answer
import subprocess
pid = subprocess.check_output(["xdotool", "getactivewindow", "getwindowpid"]).decode("utf-8").strip()
print(pid)
but you'd need to install xdotool
:
sudo apt install xdotool
Get the name of the WM_CLASS
We can even surpass the pid:
#!/usr/bin/env python3
import subprocess
w_id = subprocess.check_output(["xdotool", "getactivewindow"]).decode("utf-8")
name = subprocess.check_output(["xprop", "-id", w_id, "WM_CLASS"]).decode("utf-8").strip()
print(name)
Mind though that the WM_CLASS often, but not necessarily always is the process name.
-
I borrowed the 'getactivewindow' part of your answer as an expansion upon my
psutil
suggestion. Sorry, but I hope you don't mind, I credited you in the answer a bit :)– Thomas Ward ♦Apr 27, 2017 at 19:02 -
i need small help, i want my python code to do something whenever the active window change, one way of doing it is using infinite while loop . but there must be some better way of doing it. how can i do it. how do i keep my python code alive while listening to if active window is changed?. Mar 21, 2021 at 6:36
-
-
-
@AayushNeupane Ah, right. Please ask a question, I (or anyone else) will answer. I think we shouldn't post it on this question. Please ping me if you do. Mar 21, 2021 at 21:28
import wnck
screen = wnck.screen_get_default()
window = screen.get_active_window()
pid = window.get_pid()
-
This gets the ID of the process, but not the process' name. If you know the process ID, however, you could then open the System Monitor and match the ID to the process name in the table under the "Processes" tab. Aug 7, 2012 at 21:19
libreoffice
<>soffice.bin
etc)