用AppleScript做macOS UI自动化

用AppleScript做macOS UI自动化

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

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

bash 复制代码
tell application "System Settings"

  get every anchor of current pane

end tell

2. 滑动到Extensions 区域

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

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

bash 复制代码
tell application "System Settings"
	activate
	reveal anchor "ExtensionItems" of pane id "com.apple.LoginItems-Settings.extension"
end tell

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

期望点[Trend Micro Antivirus Extension]的按钮,所以先根据Text 定位到[Trend Micro Antivirus Extension],再找到他同层级的下一个元素并点击

bash 复制代码
-- 启用辅助功能
tell application "System Events"
	-- 指定目标应用程序
	tell process "System Settings"
		-- 打开目标窗口
		set frontmost to true
		delay 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 false
		set nextElement to missing value
		-- 遍历所有同层次元素,找到目标元素并获取其下一个元素
		repeat with i from 1 to count of siblingElements
			set currentElement to item i of siblingElements
			if value of currentElement contains "Trend Micro Antivirus Extension" then
				set targetElementFound to true
			else if targetElementFound then
				set nextElement to currentElement
				exit repeat
			end if
		end repeat
		-- 点击目标元素的下一个元素的信息
		if nextElement is not missing value then
			perform action "AXPress" of nextElement
		else
			return "not found button"
		end if
	end tell
end tell

三、获取元素位置并点击

通过UI Browser 可以很快得到element

这里拿到元素坐标,然后用pyautogui.click(),具体见上一篇
AppleScript点击事件无效的问题排查记录

相关推荐
灵感菇_37 分钟前
Flutter Riverpod 完整教程:从入门到实战
前端·flutter·ui·状态管理
2022.11.7始学前端3 小时前
n8n第七节 只提醒重要的待办
前端·javascript·ui·n8n
2501_941982054 小时前
RPA 的跨平台部署与统一自动化策略
运维·自动化·rpa
b***25114 小时前
电池自动分选机:精密分选保障新能源产业质量核心
运维·自动化·制造
尤老师FPGA4 小时前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第四十讲)
ui
成富5 小时前
Chat Agent UI,类似 ChatGPT 的聊天界面,Spring AI 应用的测试工具
java·人工智能·spring·ui·chatgpt
御控工业物联网5 小时前
二次供水设备及一体化泵站物联网解决方案:构建智慧水务的“神经中枢”
物联网·自动化·plc·智慧水务·远程操控plc
测试人社区—84166 小时前
Postman API测试指南
人工智能·git·测试工具·自动化·bug·postman
北京盟通科技官方账号6 小时前
Ixxat Mobilizer系列:助力汽车组件的高效下线测试
网络协议·机器人·自动化·汽车·制造
秋刀鱼 ..6 小时前
第五届大数据经济与数字化管理国际学术会议(BDEDM 2026)
大数据·运维·人工智能·python·机器人·自动化