Here is how you can retrieve the title of the foreground window in pure Python, with no external libraries needed! Just the built-in ctypes! :-) But that library is 6% slower than my code.))ĭocumentation is here: (You must read its usage help if you wanna write your own code, otherwise you can cause segmentation fault crashes, hehe.)īasically, ctypes includes bindings for the most common Windows DLLs. ((Update in late 2020: The dead win32gui library has come back to life with a rename to pywin32, so if you want a maintained library, it's now a valid option again. And it's a way cleaner interface than old and dead libraries like win32gui (last updated in 2017 as of this writing). The ctypes library is included with Python since v2.5, which means that almost every user has it. I have written an example implementation using his hints. Thanks goes to the answer by Nuno André, who showed how to use ctypes to interact with Windows APIs. Print('Active window: %s' % str(get_active_window())) Logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', Make sure AppKit is available The script #!/usr/bin/env python
Install wnck ( sudo apt-get install python-wnck on Ubuntu, see libwnck.) It is currently only tested on Linux (Ubuntu Mate Ubuntu 15.10). The following script should work on Linux, Windows and Mac.