写点什么

使用 AppleScript 批量删除 Mac 中的信息

作者:CRMEB
  • 2022 年 3 月 09 日
  • 本文字数:3557 字

    阅读完需:约 12 分钟

涉及工具

1 mac 自带 app:“脚本编辑器” 2 原生应用「Accessibility Inspector(需安装 Xcode)」用以定位目标控件的类型,方便在打印的子控件中查找

开始:

模拟操作步骤:

打开 message app

tell application "Messages" to activate复制代码
复制代码

操作需要在 System Events 下执行,所以需要 tell 一下

tell application "Messages" to activatetell application "System Events"
end tell复制代码
复制代码

找到“信息”app

tell application "Messages" to activatetell application "System Events"	tell process "Messages"
end tellend tell复制代码
复制代码

查找选中要删除的短信

经验性规律:脚本运行结果中的所有 UI 元素是按软件界面中从上到下,从左到右的顺序排列的。

结合 Accessibility Inspector ,查找要删除的短信的 path

注意,大家的 path 可能不一样,比我有两个顶置消息所以位置 path 如下

tell application "Messages" to activatetell application "System Events"	tell process "Messages"				click UI element 4 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of window 1			end tellend tell	复制代码
复制代码

用“信息”app 的顶部菜单栏实现,触发删除操作

查找顶部菜单栏中的删除按钮

delay 给出系统响应和 UI 事件的时间

tell application "Messages" to activatetell application "System Events"	tell process "Messages"				click UI element 4 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of window 1		delay 0.2		tell menu 1 of menu bar item "文件" of menu bar 1			entire contents		end tell			end tellend tell	复制代码
复制代码

打印

{menu item "新信息" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item 2 of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "快速查看" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item 4 of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "关闭窗口" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "全部关闭" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "删除对话…" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "在单独窗口中打开对话" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item 9 of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "从iPhone导入" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu "从iPhone导入" of menu item "从iPhone导入" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "iPhone 13" of menu "从iPhone导入" of menu item "从iPhone导入" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "拍照" of menu "从iPhone导入" of menu item "从iPhone导入" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "扫描文稿" of menu "从iPhone导入" of menu item "从iPhone导入" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "添加速绘" of menu "从iPhone导入" of menu item "从iPhone导入" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item 5 of menu "从iPhone导入" of menu item "从iPhone导入" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item 11 of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events", menu item "打印…" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Messages" of application "System Events"}
复制代码

找到目标的 path :menu item "删除对话…" of menu "文件" of menu bar item "文件" of menu bar 1

click 它,弹出删除确认框

弹出删除确认框

如果不熟悉 Mac 端的开发控件,可通过 Accessibility Inspector,点击右上角聚焦,选中控件,查看控件信息

弹出框为 sheet 类型,在新 window 中

tell application "Messages" to activatetell application "System Events"	tell process "Messages"				click UI element 4 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of window 1		delay 0.2		click menu item "删除对话…" of menu 1 of menu bar item "文件" of menu bar 1		delay 0.2		tell sheet 1 of window 1			entire contents		end tell			end tellend tell	复制代码
复制代码

打印

{image 1 of sheet 1 of window "1068177100319040" of application process "Messages" of application "System Events", static text "您要删除此对话吗?" of sheet 1 of window "1068177100319040" of application process "Messages" of application "System Events", static text "此对话将从您的所有设备上删除。您不能撤销此操作。" of sheet 1 of window "1068177100319040" of application process "Messages" of application "System Events", button "删除" of sheet 1 of window "1068177100319040" of application process "Messages" of application "System Events", button "取消" of sheet 1 of window "1068177100319040" of application process "Messages" of application "System Events"}
复制代码

找目标 item 的 path :button "删除" of sheet 1

tell application "Messages" to activatetell application "System Events"	tell process "Messages"		click UI element 4 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of window 1		delay 0.2		click menu item "删除对话…" of menu 1 of menu bar item "文件" of menu bar 1		delay 0.2		click button "删除" of sheet 1 of window 1			end tellend tell复制代码
复制代码

重复,删除全部

tell application "Messages" to activatetell application "System Events"	tell process "Messages"				repeat 2 times			click UI element 4 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 2 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of window 1			delay 0.2			click menu item "删除对话…" of menu 1 of menu bar item "文件" of menu bar 1			delay 0.2			click button "删除" of sheet 1 of window 1			delay 0.5		end repeat			end tellend tell
复制代码

最后

如果你觉得此文对你有一丁点帮助,点个赞。或者可以加入我的开发交流群:1025263163 相互学习,我们会有专业的技术答疑解惑

如果你觉得这篇文章对你有点用的话,麻烦请给我们的开源项目点点 star:http://github.crmeb.net/u/defu不胜感激 !

PHP 学习手册:https://doc.crmeb.com

技术交流论坛:https://q.crmeb.com

用户头像

CRMEB

关注

还未添加个人签名 2021.11.02 加入

CRMEB就是客户关系管理+营销电商系统实现公众号端、微信小程序端、H5端、APP、PC端用户账号同步,能够快速积累客户、会员数据分析、智能转化客户、有效提高销售、会员维护、网络营销的一款企业应用

评论

发布
暂无评论
使用AppleScript批量删除Mac中的信息_CRMEB_InfoQ写作平台