Python Forum
Current foreground application - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Current foreground application (/thread-27063.html)



Current foreground application - tibegato - May-24-2020

Trying to figure out the name of the current foreground application running, in Linux. Having things like the PID and Windowtitle are nice ... But, I need the application name itself. Like Chrome, nano, vim, etc ... If anyone knows how to do it in a bash script that'd be great also. But, I'll gladly take a python script.


RE: Current foreground application - bowlofred - May-24-2020

I'm not sure what you're looking for by "the" foreground application. I might have multiple foreground applications running in multiple windows. There's no easy way in Linux to distinguish between them.

The psutil package has methods for going from PID to process name, and finding process parents/children, but I'm not sure which PID you are interested in.


RE: Current foreground application - tibegato - May-24-2020

I mean, whatever application has focus. Like now, I'm in Chrome making this reply. So, Chrome is focused and is in the foreground.

I'll check out library you mentioned.


RE: Current foreground application - bowlofred - May-24-2020

That will depend on the window manager and whether you can poll it and get an answer. I don't know an easy way to do that (either with or without python).


RE: Current foreground application - tibegato - May-25-2020

Well, I'm using i3, with polybar ontop of it. I can use linux commands to get a list of windows. wmctrl -l ... In polybar, there's a module that gets the window title of the "active" window. It's written in c++ and I don't know c++.