【问题标题】:Obtain list of all window titles on macOS from a Python script从 Python 脚本获取 macOS 上所有窗口标题的列表
【发布时间】:2018-11-10 08:29:02
【问题描述】:

我希望能够从 Python 脚本中获取 macOS 上所有窗口标题的字符串列表。在 Windows 上,有一个 win32 api(enumWindows() 函数)可以做到这一点;我想要 macOS 等价物。

这可能吗?我想我需要使用 pyobjc。

【问题讨论】:

标签: python macos pyobjc


【解决方案1】:

以下脚本基于 Mark Setchell 的评论并打印应用程序名称和窗口名称(使用 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”方法来访问窗口名称。

【讨论】:

    猜你喜欢
    • 2017-06-20
    • 1970-01-01
    • 2011-11-08
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 2010-12-05
    相关资源
    最近更新 更多