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

相关推荐
todoitbo6 小时前
用虚拟局域网打通 Win/Mac/Linux 三端:跨设备协作的实用方案
linux·运维·macos
星纬智联技术8 小时前
GEO E2E 自动化验证测试文章
运维·自动化·geo
爱敲代码的菜菜10 小时前
【测试】自动化测试
css·selenium·测试工具·junit·自动化·xpath
奶茶精Gaaa10 小时前
AI实战(二)生成ui自动化
功能测试·学习·自动化
厦门辰迈智慧科技有限公司12 小时前
高边坡自动化监测要点分析
自动化·高边坡监测·边坡监测
姓王名礼13 小时前
一份 Windows/macOS/Linux 完整安装 + 运行 + 对接 WebUI 的步骤
linux·windows·macos
老星*14 小时前
Lucide Icons:开源、轻量、设计师友好的现代图标库
ui·开源·github
rosmis15 小时前
自动化文献检索与下载工作流:基于 Playwright 的 RPA 实践方案
运维·自动化·rpa
Swift社区15 小时前
AI 驱动 UI:鸿蒙 ArkUI 的新可能
人工智能·ui·harmonyos
柯儿的天空15 小时前
【OpenClaw 全面解析:从零到精通】第 016 篇:OpenClaw 实战案例——代码开发助手,从代码生成到部署自动化的全流程
运维·人工智能·ai作画·自动化·aigc·ai写作