从Python脚本获取macOS上所有窗口标题的列表 [英] Obtain list of all window titles on macOS from a Python script
问题描述
我希望能够从Python脚本获取macOS上所有窗口标题的字符串列表.在Windows上,有一个win32 api(enumWindows()函数)可以做到这一点.我想要等效的macOS.
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.
这可能吗?我假设我需要使用pyobjc.
Is this possible? I assume I'll need to use pyobjc.
推荐答案
以下脚本基于Mark Setchell的注释,并打印应用程序名称和窗口名称(使用Python 3.7):
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>')}")
请注意,窗口可能没有名称,因此使用"get"方法来访问窗口名称.
Note that windows may not have a name, hence the use of the "get" method to access the window name.
这篇关于从Python脚本获取macOS上所有窗口标题的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!