如何在 applescript 中与 "Messages" 交互

How to interact with "Messages" in applescript

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

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 元素的相关信息。任何帮助将不胜感激。

如果您尝试单击工具栏上的对话详细信息按钮 =24=]Messages in macOS Big Sur with AppleScript, 然后是下面的 example AppleScript code 会做到:

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 语句 .