7

I am writing a desktop usage statistics app. It runs a background daemon which wakes up at regular intervals, finds the name of the application window currently in focus and logs that data in database.

I manage to do this on Linux desktop with help of xprop utility (you can find it here).

How can I do the same on Mac OSX? Is there a direct/indirect way to do this from a python script? (PyObjC?)

Jayesh
  • 51,787
  • 22
  • 76
  • 99
  • Currenly implementing exactly same thing, so would like to know answer regarding MacOSX as well. As for linux, you can use python-xlib package to get this info w/out calling other executables, like this import Xlib.display Xlib.display.Display().get_input_focus().focus.get_wm_name() – Daniel Kluev Jul 24 '10 at 08:27

2 Answers2

3

It should be possible to get the active window by using AppKit like the following

from AppKit import NSWorkspace

workspace = NSWorkspace.sharedWorkspace()
active_app = workspace.activeApplication()['NSApplicationName']
jmlw
  • 91
  • 1
  • 3
2

You can do this with AppleScript:

Get the title of the current active Window/Document in Mac OS X

You can try using appscript to generate AppleScript events from python.

Community
  • 1
  • 1
Matthew Schinckel
  • 35,041
  • 6
  • 86
  • 121