0

I'd like to be able to obtain a list of strings of all the window titles on macOS from a Python script. On Windows, there's a win32 api (the enumWindows() function) that can do this; I'd like the macOS equivalent.

Is this possible? I assume I'll need to use pyobjc.

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92

1 Answers1

2

The following script is based on the comment by Mark Setchell and prints application names and window names (using Python 3.7):

import Quartz

windows = Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListExcludeDesktopElements|Quartz.kCGWindowListOptionOnScreenOnly,Quartz.kCGNullWindowID);

for window in windows:
    print(f"{window[Quartz.kCGWindowOwnerName]}: {window.get(Quartz.kCGWindowName, '<no name>')}")

Note that windows may not have a name, hence the use of the "get" method to access the window name.

Ronald Oussoren
  • 2,715
  • 20
  • 29