关于applescript:使用AppleScript批量删除Mac中的信息

41次阅读

共计 5287 个字符,预计需要花费 14 分钟才能阅读完成。

波及工具
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

正文完
 0