用于删除与正则表达式

时间:2017-11-21 01:00:58

标签: regex sms applescript messages

我正在尝试创建一个AppleScript来删除与特定正则表达式模式匹配的消息对话。我试图效仿我发现的一个例子,但它似乎使用的代码不再适用(可能是High Sierra的限制)。

以下是代码似乎无法正常工作。

set chatsToKill to {}
tell application "Messages"
    set allChats to every chat
    repeat with eachChat in allChats
    --
    -- The example fails because ScriptEditor would error saying it could get participants of the value returned for the first (or any of eachChat).
    --
    set thePeeps to participants of eachChat
    repeat with oneParticipant in thePeeps
        set theHandle to handle of oneParticipant
        try
            do shell script "echo " & quoted form of theHandle & " | egrep '^SMS;-;141'"
            if chatsToKill does not contain eachChat's id then set end of chatsToKill to eachChat's id
        end try
    end repeat
end repeat
repeat with deathChat in chatsToKill
    -- This should be a delete of the deathChat
end repeat
end tell

1 个答案:

答案 0 :(得分:0)

也许自编写脚本以来事情已经发生了变化,但它有缺陷并且不起作用,我不确定它是否可以像macOS 10.11.6 +一样工作。

Apple极大地限制了对消息应用的脚本访问权限。

当我单步执行您的脚本时,我在此行中收到错误:
set thePeeps to participants of eachChat

错误是:

  

消息收到错误:无法获得文字聊天ID的参与者   "的iMessage; - ;。+ 1123121234"

即使我将代码放在try / catch块中,所有消息也都失败了。

即使这样有效,我也不知道你的脚本的其余部分是如何工作的。您执行do shell script而不将结果分配给任何内容。

你需要这样的东西:

    set scriptResults to do shell script "echo " & quoted form of theHandle & " | egrep '^SMS;-;141'"
    -- do something with scriptResults

    if chatsToKill does not contain eachChat's id then set end of chatsToKill to eachChat's id

抱歉,我无法提供更好的服务。