欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 用AppleScript做macOS UI自动化

用AppleScript做macOS UI自动化

2025/2/24 10:11:40 来源:https://blog.csdn.net/qq_34495095/article/details/141566252  浏览:    关键词:用AppleScript做macOS UI自动化

用AppleScript做macOS UI自动化

    • 一、定位到System Setting → General → Login Items& Extensions 页面
      • 1. 获取页面锚点,以便直接滑动到锚点区域
      • 2. 滑动到Extensions 区域
    • 二、根据名称找到元素,再点击元素的按钮
    • 三、获取元素位置并点击

一、定位到System Setting → General → Login Items& Extensions 页面

1. 获取页面锚点,以便直接滑动到锚点区域

tell application "System Settings"get every anchor of current paneend tell

在这里插入图片描述

2. 滑动到Extensions 区域

reveal命令用于在“系统设置”中显示指定的窗格或部分

anchor关键字则用于指定窗格内的一个特定部分或标签页。

tell application "System Settings"activatereveal anchor "ExtensionItems" of pane id "com.apple.LoginItems-Settings.extension"
end tell

二、根据名称找到元素,再点击元素的按钮

期望点[Trend Micro Antivirus Extension]的按钮,所以先根据Text 定位到[Trend Micro Antivirus Extension],再找到他同层级的下一个元素并点击
在这里插入图片描述

-- 启用辅助功能
tell application "System Events"-- 指定目标应用程序tell process "System Settings"-- 打开目标窗口set frontmost to truedelay 1 -- 给窗口一些时间来加载-- 获取目标窗口set targetWindow to window "Login Items & Extensions"-- 获取目标元素的所有兄弟元素(同层次元素)set siblingElements to UI elements of group 3 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of targetWindow-- 初始化变量set targetElementFound to falseset nextElement to missing value-- 遍历所有同层次元素,找到目标元素并获取其下一个元素repeat with i from 1 to count of siblingElementsset currentElement to item i of siblingElementsif value of currentElement contains "Trend Micro Antivirus Extension" thenset targetElementFound to trueelse if targetElementFound thenset nextElement to currentElementexit repeatend ifend repeat-- 点击目标元素的下一个元素的信息if nextElement is not missing value thenperform action "AXPress" of nextElementelsereturn "not found button"end ifend tell
end tell

三、获取元素位置并点击

在这里插入图片描述

通过UI Browser 可以很快得到element
在这里插入图片描述
这里拿到元素坐标,然后用pyautogui.click(),具体见上一篇
AppleScript点击事件无效的问题排查记录

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词