finding the app window currently in focus on Mac OSX
Asked Answered
A

2

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?)

Ashwin answered 24/7, 2010 at 8:20 Comment(1)
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()Boito
R
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']
Rhetic answered 29/3, 2022 at 14:16 Comment(0)
F
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.

Feverfew answered 24/7, 2010 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.