用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 小时前
iOS中广告SDK如何判断一个广告是否真实展示
macos·ios·cocoa
大雷神1 小时前
MateChat+ DevUI 电商后台管理系统中集成 AI 聊天助手功能
人工智能·ui
冒泡P2 小时前
【Unity】TextMeshPro富文本中使用精灵图集
ui·unity·c#·游戏引擎
B0URNE4 小时前
【Unity基础详解】(9)Unity核心:UI系统
ui·unity·游戏引擎
机器人行业研究员4 小时前
六维力传感器和关节力传感器国产替代正当时:机器人“触觉神经”的角逐
机器人·自动化·人机交互·六维力传感器·关节力传感器
梵克之泪5 小时前
【号码分离】从Excel表格、文本、word文档混乱文字中提取分离11位手机号出来,基于WPF的实现方案
开发语言·ui·c#
心灵宝贝9 小时前
Mac 安装 JDK 8u281(JDK-8u281-1.dmg)详细步骤(附安装包)
java·macos·intellij-idea
EAIReport10 小时前
自动化报告生成产品内嵌OA/BI平台:解决传统报告痛点的技术方案
java·jvm·自动化
小小测试开发15 小时前
JMeter JSR223预处理程序全攻略:用Groovy解锁复杂场景自动化
运维·jmeter·自动化
侯小啾17 小时前
理解计算机网络中的MAC地址
网络·计算机网络·macos·mac