用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点击事件无效的问题排查记录

相关推荐
牛马十年10 小时前
当自动化运维系统被ai重构后
运维·人工智能·自动化
莞凰10 小时前
昇腾CANN的“传音入密“:hccl仓库探秘
flutter·ui·transformer
dayuOK630711 小时前
AI内容创作工具的下一个战场:从“生成”到“全流程自动化”
运维·人工智能·chatgpt·职场和发展·自动化·新媒体运营·媒体
Agent手记11 小时前
成本数据多系统自动采集与分析实操指南:基于2026大模型Agent的超自动化实践
运维·人工智能·microsoft·ai·自动化
拉卡拉开放平台11 小时前
支付系统在文旅场景的进阶之路:聚合收单、分账与自动化对账
大数据·人工智能·自动化
tedcloud12312 小时前
hello-agents部署教程:从零学习AI Agent开发
服务器·人工智能·学习·自动化·powerpoint
Agent产品评测局13 小时前
新能源制造供应链AI方案主流产品对比测评 —— 2026年企业级自动化选型深度指南
人工智能·ai·chatgpt·自动化·制造
码点滴13 小时前
Workload 自动化进化论:从手动运维到 AI 驱动的 Kubernetes 智能管控
运维·人工智能·kubernetes·自动化·workload
孙高飞14 小时前
直播预告 - 周日晚 7 点半-AI 驱动 UI 自动化
运维·自动化
小贺儿开发14 小时前
一句话生成网页 + 自动化办公(OpenCode + DeepSeek-V4)
css·自动化·html·工具·代码·网页·deepseek