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

相关推荐
李小白杂货铺1 天前
Oh My Zsh 简记
macos·macbook·zsh·主题·插件·oh my zsh·omz
miaowu3571 天前
AI智能体推动数字化转型:从流程自动化到决策辅助的完整路线图
大数据·人工智能·自动化
QYR-分析1 天前
智能化自动化浪潮下,机器人末端执行器行业赛道发展全景解析
人工智能·机器人·自动化
workflower2 天前
模型中心主义路径
大数据·人工智能·深度学习·机器学习·自动化·制造
JaneConan2 天前
鸿蒙 ArkUI 深水区:@Watch 和 @Observed,状态变了「自动跑」+ 嵌套对象「深层重绘」
开发语言·后端·ui·harmonyos
Urbano2 天前
卫衣全工序自动化智造科普:替代工位、设备选型与产能升级方案
大数据·人工智能·自动化
施棠海2 天前
Pixel2XML实战:10分钟从设计稿做出一个能拨号的Android拨号盘页面(附完整业务代码)
android·java·ui
施棠海2 天前
设计稿一键变成可运行的Android页面:Pixel2XML全流程UI开发框架实战(附完整源码)
android·ui·架构
YangYang9YangYan2 天前
2026自动化背景无项目经验转数据分析的可行性分析
运维·数据分析·自动化
fukai77222 天前
macOS防止休眠的菜单栏小工具
macos