11

I want to write a python script on Windows that saves the title of the program that the user uses at a given time like the http://www.rescuetime.com . I don't want to use rescuetime because of privacy consideration but instead write a script that does something similar myself to capture data about how much I use my computer.

Is there some easy command that I can use to read that information?

Christian
  • 25,249
  • 40
  • 134
  • 225
  • 1
    Answered here - http://stackoverflow.com/questions/10266281/obtain-active-window-using-python – SuperManu Feb 17 '17 at 07:41
  • Does this answer your question? [Obtain Active window using Python](https://stackoverflow.com/questions/10266281/obtain-active-window-using-python) – user202729 Jun 03 '23 at 03:02

2 Answers2

23

From daniweb

  import win32gui
  w=win32gui
  w.GetWindowText (w.GetForegroundWindow())
Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
3

It might already be too late, but here's what should work on most operating systems:

import pyautogui
window_title = pyautogui.getActiveWindowTitle()

(I'm not sure if it works for Ubuntu, but Mac Os, Linux and Windows are normally supported)

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Daveloper
  • 31
  • 2
  • pyautogui seems to have quite some issues when trying to install. I'm on mac os and the pyobjc-core package failed to install no matter what I tried. –  Mar 06 '22 at 21:08
  • `import pygetwindow` instead. I guess the feature is not ready to merge into `pyautogui` – Tuan Chau Apr 05 '22 at 01:47