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

相关推荐
monsion5 小时前
AG-UI 协议详解:Agent 与用户之间缺失的那一层
人工智能·ui·个人开发
pop_xiaoli6 小时前
【iOS】离屏渲染
macos·ios·objective-c·cocoa
苏州邦恩精密6 小时前
浙江蔡司3D扫描仪应用领域及厂家选择分析
人工智能·科技·3d·自动化·制造
Full Stack Developme7 小时前
flowable-spring-boot-starter 与 flowable-ui
ui
一键生成网站8 小时前
AI生成UI原型支持复杂逻辑交互工具对比与场景适配分析
人工智能·ui·交互·
糯米导航17 小时前
AI 视觉回归实战:截图对比不是“找不同”,如何让智能差异分析真正服务 UI 质量
人工智能·ui·回归
其实防守也摸鱼19 小时前
运维--学习阶段问题解答(1)(自测)
linux·运维·服务器·数据库·学习·自动化·命令模式
GJGCY20 小时前
财务智能体落地实践:RPA、大模型与规则引擎如何构建业财自动化闭环?
经验分享·ai·自动化·财务·智能体
CedarQR1 天前
VS Code Copilot Chat 配置 MCP Server 全攻略(以 Next AI Draw.io 为例)
人工智能·ui·json
博观而约取厚积而薄发1 天前
Pytest 从入门到精通,一篇就够(超详细实战教程)
python·测试工具·单元测试·自动化·pytest