macOS 10.15.6下使用AppleScript实现FaceTime音频自动呼叫失效问题
10.15系统下FaceTime音频自动呼叫失效解决方案
问题根因
Catalina(10.15)对FaceTime音频模式的UI层级做了调整:通过facetime-audio://协议调起的呼叫,呼叫确认弹窗是主窗口的Sheet(临时内嵌弹窗),而非独立的顶级窗口元素。原代码仅在主窗口的顶层子元素中查找「Call」按钮,无法定位到弹窗内的实际呼叫按钮,因此脚本执行后仍需要手动点击。
而视频模式的UI结构未做调整,原逻辑可正常匹配到对应按钮。
修正后的可直接运行代码
-- 替换为你需要呼叫的目标号码/Apple ID,国内手机号建议加国家码前缀如+8613xxxxxxxxx set callee to "你的目标呼叫账号" -- 调起FaceTime音频呼叫 do shell script "open facetime-audio://" & callee tell application "System Events" to tell process "FaceTime" -- 强制激活FaceTime到前台 set frontmost to true set btnFound to false -- 最多10秒内遍历所有可能的按钮位置,避免窗口加载延迟 repeat 10 times -- 匹配主窗口顶层的Call按钮(兼容旧版逻辑) try if exists button "Call" of window 1 then click button "Call" of window 1 set btnFound to true exit repeat end if end try -- 匹配主窗口Sheet弹窗内的Call按钮(10.15音频模式的默认位置) try if exists button "Call" of sheet 1 of window 1 then click button "Call" of sheet 1 of window 1 set btnFound to true exit repeat end if end try -- 兜底逻辑:遍历窗口下所有层级的元素,匹配名为Call的按钮 try set matchedBtns to entire contents of window 1 whose name is "Call" if (count of matchedBtns) > 0 then click item 1 of matchedBtns set btnFound to true exit repeat end if end try delay 1 end repeat -- 未找到按钮时弹出提示,方便排查问题 if not btnFound then display dialog "未检测到呼叫按钮,请检查FaceTime窗口状态" end if end tell
配套排查注意事项
- 权限配置:10.15对UI控制权限管控严格,需要给运行这段脚本的载体(脚本编辑器、调用AppleScript的Python进程)都在「系统偏好设置-安全性与隐私-隐私-辅助功能」中开启权限,否则即使定位到按钮也无法执行点击操作。
- 号码格式:目标手机号需添加对应国家码前缀,否则FaceTime可能无法正确识别账号,弹出的窗口结构会偏离预期。
- 默认呼叫设置:检查FaceTime偏好设置的「音频」栏,确认默认呼叫方式为音频,避免音频协议调起后自动跳转至视频呼叫流程。
- Python调用适配:如果需要从Python应用触发呼叫,可通过
subprocess模块调用系统的osascript命令传入AppleScript代码,运行前确保Python进程本身已获得辅助功能权限,示例调用代码:import subprocess applescript = """ set callee to "你的目标呼叫账号" do shell script "open facetime-audio://" & callee tell application "System Events" to tell process "FaceTime" set frontmost to true set btnFound to false repeat 10 times try if exists button "Call" of window 1 then click button "Call" of window 1 set btnFound to true exit repeat end if end try try if exists button "Call" of sheet 1 of window 1 then click button "Call" of sheet 1 of window 1 set btnFound to true exit repeat end if end try try set matchedBtns to entire contents of window 1 whose name is "Call" if (count of matchedBtns) > 0 then click item 1 of matchedBtns set btnFound to true exit repeat end if end try delay 1 end repeat end tell """ subprocess.run(["osascript", "-e", applescript])
内容的提问来源于stack exchange,提问作者JAKB824
相关产品推荐
相关产品推荐

