从左下角的 Windows Media Player UI 获取当前歌曲标题

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

我想从 Windows Media Player 左下角的 UI 元素获取文本。我想将它存储在 python 变量中。如何以编程方式导航 UI 层次结构来实现此目的?

python ui-automation windows-media-player microsoft-ui-automation comtypes
1个回答
0
投票

首先我们需要获取元素。 Accessibility Insights 计划可在以下方面提供帮助:

我们知道我们正在寻找一个名称为“metadata”的编辑控件 我为此使用了 jupyter 笔记本。

UIAutomationCore = comtypes.client.GetModule("UIAutomationCore.dll")
IUIAutomation = comtypes.client.CreateObject("{ff48dba4-60ef-4201-aa87-54103eef594e}", 
                                            interface=UIAutomationCore.IUIAutomation)
ViewWalker = IUIAutomation.RawViewWalker

desktop = IUIAutomation.GetRootElement()
ViewWalker.getFirstChildElement(desktop)

# https://github.com/microsoft/accessibility-insights-windows/issues/1122#issuecomment-834145895
# Thanks yinkaisheng!
def WalkTree(top, max_depth: int = 0xFFFFFFFF):
    if max_depth <= 0:
        return
    child = ViewWalker.GetFirstChildElement(top)
    childList = [child]
    depth = 0
    while depth >= 0:
        lastItem = childList[-1]
        if lastItem:
            yield lastItem, depth + 1
            child = ViewWalker.GetNextSiblingElement(lastItem)
            childList[depth] = child
            if depth + 1 < max_depth:
                child = ViewWalker.GetFirstChildElement(lastItem)
                if child:
                    depth += 1
                    childList.append(child)
        else:
            del childList[depth]
            depth -= 1

for x, depth in WalkTree(desktop, max_depth=1):
    if x.CurrentClassName == "WMPlayerApp":
        wmp = x

assert wmp

for ele, depth in WalkTree(wmp, max_depth=4):
    if ele.CurrentName == "metadata":
        print(ele.CurrentName)
        break

元数据

现在我们有了包含

ele
<POINTER(IUIAutomationElement) ptr=0x24c5ee4baf0 at 24c61e1e140>

变量

https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomationelement

https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomationtreewalker

接下来,我们需要从中获取 ValuePattern 值。

# Import the IValuePattern interface definition from UIAutomationCore
from comtypes.gen.UIAutomationClient import IUIAutomationValuePattern

# Cast the IUnknown pointer to IValuePattern
value_pattern_interface = value_pattern.QueryInterface(IUIAutomationValuePattern)

# Now you can use value_pattern_interface to get the value
current_value = value_pattern_interface.CurrentValue
print(f"Value: {current_value}")

价值:适合你的声音

(在我写这篇文章时歌名已经改变)

一个简单的测试程序:

from time import sleep
while True:
    print(value_pattern_interface.CurrentValue)
    sleep(1)

© www.soinside.com 2019 - 2024. All rights reserved.