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

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

开始:
模拟操作步骤:

打开message app

tell application "Messages" to activate
复制代码

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

tell application "Messages" to activate
tell application "System Events"

end tell
复制代码

找到“信息”app

tell application "Messages" to activate
tell application "System Events"
    tell process "Messages"

    end tell
end tell
复制代码

查找选中要删除的短信

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

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

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

tell application "Messages" to activate
tell 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 tell
end tell    
复制代码

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

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

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

tell application "Messages" to activate
tell 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 tell
end 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 activate
tell 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 tell
end 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 activate
tell 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 tell
end tell
复制代码

重复,删除全部

tell application "Messages" to activate
tell 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 tell
end tell

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

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

PHP学习手册:https://doc.crmeb.com
技术交流论坛:https://q.crmeb.com

你可能感兴趣的:(applescript)