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

相关推荐
天远数科35 分钟前
零信任架构实战:基于天远贷前风险报告构建自动化消金信贷风控与多头共债排查网关
运维·人工智能·架构·自动化
ndsc_d5 小时前
AI生成的UI如何继续修改?画布内生成与独立AI设计工具比较
人工智能·ui·ai·ui设计·ai设计·ai设计工具·ai生成ui
朝阳5815 小时前
Mac 上找窗口,可以先按这几种情况来
macos
看着博客敲代码5 小时前
WhatWeb 报错 error: cannot load such file -- whatweb 与 Web 指纹识别用法
安全·网络安全·自动化
Rosanci6 小时前
AI 编程工具该知道什么、能做什么?从仓库上下文到测试执行的权限分层
人工智能·自动化·ai 编程
志栋智能6 小时前
预算敏感型企业的自动化之路:IT运维低成本转型
运维·人工智能·自动化
盛世宏博智慧档案6 小时前
档案库房实现自动化的恒温恒湿消毒净化关键:恒湿消毒净化一体机
运维·自动化·一体机·恒湿机
Novproxy_Z6 小时前
IPv4 与 IPv6 代理在跨境自动化场景下的选型与风险
运维·自动化·php
william_yangshun7 小时前
DigUp 实测:用多模态嵌入做 macOS 全盘语义搜索
macos·向量检索
智商网输送线配件7 小时前
自动化流水线配件采购实战:从应急补货到非标定制的供应链优化方案
大数据·运维·人工智能·自动化·智商网·流水线设备配件