You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AppleScript无法访问FaceTime UI的section元素,点击按钮失败求助

问题描述

我想要制作一个自动添加联系人到FaceTime通话的自动化脚本,使用Accessibility Inspector定位UI中的+按钮路径。但遇到以下问题:

  • 直接将Accessibility Inspector中的UI元素类型、名称/索引复制到Script Editor中,出现编译错误:Expected end of line but found number
  • 移除section 1和collection 1部分后脚本可编译,但运行时提示:无法获取window 1的scroll area 1的group 1
  • 尝试将section和collection替换为UI Element,问题仍未解决

当前代码如下:

tell application "System Events"
    tell process "FaceTime"
        click button 1 of group 1 of section 1 of collection 1 of scroll area 1 of window 1
    end tell
end tell
解决方案

1. 元素名称不匹配问题

Accessibility Inspector显示的部分元素名称,和AppleScript的System Events实际支持的命名并不完全一致:

  • section在AppleScript中没有对应的直接类型,通常需要用group或UI Element配合属性筛选
  • collection同样不是System Events认可的元素类型,多数情况下对应scroll area或嵌套的group

依赖索引定位元素非常不稳定,一旦UI布局微调就会失效,更可靠的方式是通过元素的可访问性属性(比如名称、描述)来定位。

2. 修正后的脚本

下面是通过属性定位+按钮的示例脚本,兼容性和稳定性更强:

tell application "System Events"
    tell process "FaceTime"
        -- 定位通话窗口(根据实际窗口名称调整,比如"FaceTime 通话")
        set callWindow to first window whose name contains "通话"
        if callWindow exists then
            -- 通过按钮的可访问性描述或名称定位+按钮
            set addContactButton to first button of callWindow whose accessibility description contains "添加"
            if addContactButton exists then
                click addContactButton
            else
                display dialog "未找到添加联系人的按钮"
            end if
        else
            display dialog "未找到FaceTime通话窗口,请先发起或加入通话"
        end if
    end tell
end tell

3. 调试方法

如果不确定元素的实际结构,可以用以下代码打印目标窗口的所有UI元素,从日志里找到正确的元素信息:

tell application "System Events"
    tell process "FaceTime"
        log entire contents of first window
    end tell
end tell

运行后在Script Editor的「日志」面板查看元素列表,找到+按钮的层级、名称或描述属性。

4. 必要权限设置

确保FaceTime已获得辅助功能权限:

  1. 打开「系统设置」→「隐私与安全性」→「辅助功能」
  2. 在列表中找到FaceTime并勾选

内容的提问来源于stack exchange,提问作者Jakob Steele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 03:13:17