Get Window Title or Application Name with Python
Below updated version. I'll leave the old answer, to not remove the answer that got the votes.
#!/usr/bin/env python3
import gi
gi.require_version("Wnck", "3.0")
from gi.repository import Wnck
scr = Wnck.Screen.get_default()
scr.force_update()
print(scr.get_active_window().get_name())
or get xid:
print(scr.get_active_window().get_xid())
or (not very surprising) get pid:
print(scr.get_active_window().get_pid())
Also see here to get Wnck.Window methods.
Old answer:
I'd just parse the output of either xprop
or xwit
and wmctrl
(you might have to install wmctrl
first: sudo apt-get install wmctrl
). xprop gives a lot of information on windows.
xprop -root
gives you information on the active window, a.o. the window id, and
wmctrl -l
gives you a list of currently opened windows. Using the -p
option also gives you information on the pids the windows belong to. Combined you can get all the info you need.
for example:
In python 3, using subprocess check_output():
To get the active window (id):
-using xprop
# [1]
import subprocess
command = "xprop -root _NET_ACTIVE_WINDOW | sed 's/.* //'"
frontmost = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").strip()
print(frontmost)
> 0x38060fd
-using xprop, parsing it "inside" python
# [2]
import subprocess
command = "xprop -root _NET_ACTIVE_WINDOW"
frontmost = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").strip().split()[-1]
print(frontmost)
> 0x38060fd
Once we have the window-id, get the (pid of) the application it belongs to, using wmctrl:
NB: first, we have to "fix" the frontmost id (output) of the command above for wmctrl; the id from wmctrl and xprop slightly differs:
0x381e427 (xprop)
0x0381e427 (wmctrl)
to fix the output of the function above (using the "frontmost" output of # [1]
or # [2]
):
fixed_id = frontmost[:2]+"0"+frontmost[2:]
then get the pid of the (application of the) frontmost window:
command = "wmctrl -lp"
window_pid = [l.split()[2] for l in subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").splitlines() if fixed_id in l][0]
> 6262
In python 2, using subprocess.Popen():
In python 2, subprocess.check_output is not available, so the procedure is slightly different and a bit more verbose:
To get the active window (id):
-using xprop
# [1]
import subprocess
command = "xprop -root _NET_ACTIVE_WINDOW"
output = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE)
frontmost = output.communicate()[0].decode("utf-8").strip().split()[-1]
print frontmost
> 0x38060fd
to get the (pid of the) application it belongs to, using wmctrl and the output of # [1]
-(again) using (and fixing) the output of [1]
:
# [2]
import subprocess
fixed_id = frontmost[:2]+"0"+frontmost[2:]
command = "wmctrl -lp"
output = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE)
window_pid = [l.split()[2] for l in output.communicate()[0].decode("utf-8").splitlines() if fixed_id in l][0]
print window_pid # pid of the application
> 6262
to get the window name, using wmctrl
and the output of # [1]
(also using socket.gethostname()
to split the output of wmctrl
by machine name)
# [3]
import subprocess
import socket
command = "wmctrl -lp"
output = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE)
window_list = output.communicate()[0].decode("utf-8")
window_name = [l for l in window_list.split("\n") if fixed_id in l][0].split(socket.gethostname()+" ")[-1]
print window_name
Related videos on Youtube
netzaffin
to be "netzaffin" means to stay very close to internet stuff.
Updated on September 18, 2022Comments
-
netzaffin over 1 year
I'm working with multitouch and I try to do different stuff on different applications with the same gestures.
I have a python script, basic works.
Bot how can I decide between the applications? How to get the active window title?
Thanks
Edit System Information:
- Python 2.7.6
- Ubuntu 14.04 (Unity)
-
Jacob Vlijm over 9 yearspython3 or python2?
-
netzaffin over 9 yearspatrick@pat-workstation:~$ python --version Python 2.7.6
-
netzaffin over 9 yearsI've only tested for python 2, works like a charm. This is the very first "How to" I've found so far, awesome. Thanks! +1
-
Jacob Vlijm over 9 years@netzaffin You're welcome, my pleasure.