升级至macOS Ventura后AppleScript操作Music按钮失败求助
macOS 13 Ventura下Music导出资料库AppleScript修复方案
问题背景
升级至macOS 13 Ventura后,原模拟菜单点击的AppleScript脚本无法找到「Save」按钮,执行name of every button返回{missing value, missing value, missing value},这是因为Ventura调整了系统对话框的UI层级结构。
解决方法
方法一:使用Music原生AppleScript命令(推荐)
直接调用Music内置的导出命令,无需依赖UI操作,彻底规避系统UI变更的影响:
tell application "Music" -- 替换为你实际需要保存的文件路径 set exportFilePath to POSIX file "/Users/你的用户名/Music/iTunes Library.xml" -- 导出资料库并覆盖已有文件 export library to exportFilePath with replacing end tell
该命令会直接完成导出操作,with replacing参数自动处理文件覆盖确认,无需手动点击按钮。
方法二:适配Ventura UI层级修改模拟点击脚本
若必须保留模拟菜单点击的方式,需调整元素定位逻辑并增加等待加载的步骤:
tell application "Music" activate end tell tell application "System Events" tell process "Music" -- 触发导出库菜单操作 click menu item "Library" of menu "File" of menu bar 1 click menu item "Export Library…" of menu of menu item "Library" of menu "File" of menu bar 1 -- 等待保存对话框sheet加载完成 repeat until exists sheet 1 of window 1 delay 0.5 end repeat tell sheet 1 of window 1 -- 点击保存按钮 click button "Save" -- 等待替换确认sheet加载完成 repeat until exists sheet 1 delay 0.5 end repeat -- 点击替换按钮 click button "Replace" of sheet 1 end tell end tell end tell
Ventura中保存对话框的按钮嵌套在sheet层级下,不再直接属于window 1;添加等待步骤可确保UI元素完全就绪后再执行操作,避免元素未找到的错误。
内容的提问来源于stack exchange,提问作者Gideon Av
相关产品推荐
相关产品推荐

