首页 > 解决方案 > 如何与applescript中的“消息”进行交互

问题描述

我正在尝试制作一个应用程序来自动化 Mac 上的 Messages 应用程序。其中一项自动化需要与对话信息按钮进行交互。我试图编写一个脚本来开始使用它:

tell application "System Events"
    tell process "Messages"
        set infoButton to "Conversation Details"
        click toolbar item infoButton of toolbar 1
    end tell
end tell

但是我收到错误:错误System Events got an error: Can’t get item \"Conversation Details\" of process \"Messages\"." number -1728 from item "Conversation Details" of process "Messages下面我粘贴了使用辅助功能检查器的屏幕截图,其中包含我试图操作的 ui 元素周围的信息。任何帮助将不胜感激。

在此处输入图像描述

标签: applescript

解决方案


如果您尝试 使用AppleScript单击macOS Big Sur消息工具栏上的对话详细信息 按钮,则以下示例AppleScript代码将执行此操作:

tell application "System Events" to ¬
    if exists (buttons of ¬
        toolbars of ¬
        front window of ¬
        process "Messages" whose ¬
        description is "Conversation Details") ¬
        then click (buttons of ¬
        toolbars of ¬
        front window of ¬
        process "Messages" whose ¬
        description is "Conversation Details")

简而言之,就是:

button 2 of toolbar 1 of window 1 of application process "Messages"

但是,我更喜欢用错误处理方法来编写它,就像上面显示的完整tell 语句一样。


推荐阅读