简体   繁体   中英

Obtain list of all window titles on macOS from a Python script

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM