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

相关推荐
松☆2 小时前
OpenHarmony + Flutter 混合开发高阶:实现无障碍(Accessibility)与适老化 UI 的深度集成
flutter·ui
月亮!5 小时前
移动端测试重磅升级:跨平台自动化测试框架深度对比
运维·网络·人工智能·测试工具·容器·自动化·测试用例
m_136877 小时前
n8n 企业级自动化 · 专栏完整目录树(50+ 节点级规划)
自动化·n8n
m_136877 小时前
《Code 节点实战:用 JavaScript 接管自动化系统的核心逻辑(企业级实战)》
自动化·n8n
谷粒.8 小时前
自动化测试覆盖率从30%到80%的演进历程:策略、挑战与未来展望
运维·网络·深度学习·架构·自动化·transformer·测试覆盖率
西西学代码9 小时前
Flutter中常用的UI设计
前端·flutter·ui
科普瑞传感仪器10 小时前
航空航天领域青睐:复杂曲面机器人抛光为何必须采用六维力控?
运维·人工智能·机器人·自动化·无人机
街灯L10 小时前
【Photoshop】使用画笔预设建立路径描边
ui·photoshop
谷粒.11 小时前
让缺陷描述更有价值:测试报告编写规范的精髓
java·网络·python·单元测试·自动化·log4j