问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/53237278/obtain-list-of-all-window-titles-on-macos-from-a-python-script