如何在 Mac 中使用 Python 获取活动窗口标题?

问题描述 投票:0回答:5

我正在尝试编写 Python 脚本,在 Mac OS 中使用 python 打印活动窗口的标题。

这是我的代码:

from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print active_app_name

此代码仅打印应用程序的名称,例如 Google chrome 或 firefox,但不打印标题。如何获取窗口的标题?

python macos
5个回答
8
投票

这是我使用 Python 通过 Quartz API 在 Mac OS X 上查找活动应用程序名称和窗口标题的方法。

首先我们需要根据需要添加导入:

if sys.platform == "darwin":
    import applescript
    from AppKit import NSWorkspace
    from Quartz import (
        CGWindowListCopyWindowInfo,
        kCGWindowListOptionOnScreenOnly,
        kCGNullWindowID
    )

然后我们可以通过下面的代码获取活动应用程序名称和窗口标题:

def getActiveInfo(event_window_num):
    try:
        if sys.platform == "darwin":
            app = NSWorkspace.sharedWorkspace().frontmostApplication()
            active_app_name = app.localizedName()

            options = kCGWindowListOptionOnScreenOnly
            windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
            windowTitle = 'Unknown'
            for window in windowList:
                windowNumber = window['kCGWindowNumber']
                ownerName = window['kCGWindowOwnerName']
                # geometry = window['kCGWindowBounds']
                windowTitle = window.get('kCGWindowName', u'Unknown')
                if windowTitle and (
                                event_window_num == windowNumber
                        or ownerName == active_app_name
                ):
                    # log.debug(
                    #     'ownerName=%s, windowName=%s, x=%s, y=%s, '
                    #     'width=%s, height=%s'
                    #     % (window['kCGWindowOwnerName'],
                    #        window.get('kCGWindowName', u'Unknown'),
                    #        geometry['X'],
                    #        geometry['Y'],
                    #        geometry['Width'],
                    #        geometry['Height']))
                    break

            return _review_active_info(active_app_name, windowTitle)
        if sys.platform == "win32":
            (active_app_name, windowTitle) = _getActiveInfo_Win32()
            return _review_active_info(active_app_name, windowTitle)
    except:
        log.error('Unexpected error: %s' % sys.exc_info()[0])
        log.error('error line number: %s' % sys.exc_traceback.tb_lineno)
    return 'Unknown', 'Unknown'

4
投票

无法从

NSWorkspace.sharedWorkspace().activeApplication()
访问应用程序标题。

但是你可以通过PID找到当前窗口标题:

例如:

from AppKit import NSWorkspace
pid = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationProcessIdentifier']

然后使用下面的代码找到正确的窗口(它存储在

kCGWindowOwnerPID
中),如下代码所示:

这是一个基于 @JakeW 脚本的完整 shell 示例

#!/usr/bin/python
# Prints list of windows in the current workspace.
import sys
if sys.platform == "darwin":
    from AppKit import NSWorkspace
    from Quartz import (
        CGWindowListCopyWindowInfo,
        kCGWindowListOptionOnScreenOnly,
        kCGNullWindowID
    )

if sys.platform == "darwin":
    curr_app = NSWorkspace.sharedWorkspace().frontmostApplication()
    curr_pid = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationProcessIdentifier']
    curr_app_name = curr_app.localizedName()
    options = kCGWindowListOptionOnScreenOnly
    windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
    for window in windowList:
        pid = window['kCGWindowOwnerPID']
        windowNumber = window['kCGWindowNumber']
        ownerName = window['kCGWindowOwnerName']
        geometry = window['kCGWindowBounds']
        windowTitle = window.get('kCGWindowName', u'Unknown')
        if curr_pid == pid:
            print("%s - %s (PID: %d, WID: %d): %s" % (ownerName, windowTitle.encode('ascii','ignore'), pid, windowNumber, geometry))
elif sys.platform == "win32":
    (active_app_name, windowTitle) = _getActiveInfo_Win32()

它将列出当前活动窗口的详细信息,包括其标题。


2
投票

我尝试了@kenorb的代码,但不幸的是它只能获取应用程序名称,但没有标题内容。

我终于找到了一种使用 AppleScript 来做到这一点的方法: 您可以在这里找到答案: MacOSX:获取最前面的窗口标题

首先创建一个appleScript GetNameAndTitleOfActiveWindow.scpt

global frontApp, frontAppName, windowTitle

set windowTitle to ""
tell application "System Events"
    set frontApp to first application process whose frontmost is true
    set frontAppName to name of frontApp
    tell process frontAppName
        tell (1st window whose value of attribute "AXMain" is true)
            set windowTitle to value of attribute "AXTitle"
        end tell
    end tell
end tell

return {frontAppName, windowTitle}

你可以先在你的mac终端测试一下:

osascript GetNameAndTitleOfActiveWindow.scpt

然后用Python编写:

title = subprocess.check_output(['osascript', 'GetNameAndTitleOfActiveWindow.scpt'])

1
投票

从 macOS 10.6 及更高版本开始,最好使用:frontmostApplication,如果您想获取列出的所有应用程序,可以调用 runningApplications 方法。

您可以在https://developer.apple.com/documentation/appkit/nsworkspace#overview

查看更多详细信息

例如:

from AppKit import NSWorkspace
NSWorkspace.sharedWorkspace().runningApplications() // for getting all applications
NSWorkspace.sharedWorkspace().frontmostApplication() // for active window

0
投票

尝试:

from AppKit import NSWorkspace

active_app = NSWorkspace.sharedWorkspace().activeApplication()
app_name = active_app['NSApplicationBundleIdentifier']
app_shrt_name = active_app['NSApplicationName']
© www.soinside.com 2019 - 2024. All rights reserved.