Al Sweigart
Al Sweigart

Reputation: 12939

How can I list all open windows on Mac from a Python script?

I'd like to be able to grab a list of all windows that are open on macOS/OS X from a Python script. This would be the Mac equivalent on win32's EnumWindows API call.

Ideally, I'd like to be able to use this to get a list of the title/caption text of every open window along with position/size information.

Is there an OS or Cocoa API call that will return this info?

Upvotes: 2

Views: 1798

Answers (1)

Chien-Wei
Chien-Wei

Reputation: 21

You can use pyobjc. This should get the title names for you:

from AppKit import NSWorkspace
[apps["NSApplicationName"] for apps in NSWorkspace.sharedWorkspace().launchedApplications()]

A little more background: pyobjc is implemented using Quartz Window Services, which is the program they used to manage application windows on Mac OS.

Upvotes: 2

Related Questions