Macos 重复触发“收到活动聊天信息”处理程序

Macos 重复触发“收到活动聊天信息”处理程序,macos,applescript,Macos,Applescript,虽然被问了两次,但都没有得到充分的答案,我希望我会有更好的运气 我正在编写一个AppleScript,以便在Messages.app收到消息时执行处理程序。脚本正在保存到~/Library/Application\Scripts/com.apple.iChat,并在消息首选项中设置为AppleScript处理程序 当Messages是最前面的应用程序并且收到消息时,会触发两次收到的活动聊天消息处理程序。当消息位于后台时,这似乎不是问题(接收到的消息然后触发消息接收,并且该处理程序只触发一次)。我

虽然被问了两次,但都没有得到充分的答案,我希望我会有更好的运气

我正在编写一个AppleScript,以便在Messages.app收到消息时执行处理程序。脚本正在保存到
~/Library/Application\Scripts/com.apple.iChat
,并在消息首选项中设置为AppleScript处理程序

当Messages是最前面的应用程序并且收到消息时,会触发两次收到的
活动聊天消息处理程序。当消息位于后台时,这似乎不是问题(接收到的消息然后触发
消息接收
,并且该处理程序只触发一次)。我知道触发了哪个处理程序,因为处理处理程序的部分如下所示:

using terms from application "Messages"

    on message received _msg from _sender for _chat with _text_desc
        if DEBUG then display dialog "message received"
        message_received(_sender)
    end message received

    on chat room message received _msg from _sender for _chat with _text_desc
        if DEBUG then display dialog "chat room message received"
        message_received(_sender)
    end chat room message received

    on active chat message received _msg from _sender for _chat with _text_desc
        if DEBUG then display dialog "active chat message received"
        message_received(_sender)
    end active chat message received

    -- More handlers below, mostly like the above or empty

end using terms from
我将
DEBUG
属性设置为
true
,并可以查看触发哪个处理程序

我试图通过编写一个临时文件来解决这个问题(使用
\u发送方的UUID
)。收到的
消息\u
处理程序检查文件是否存在,如果文件存在,则不应执行任何操作。但这并没有奏效,即使是随机延迟。我尝试延长随机延迟的长度,但这会导致AppleScript运行超过10秒的错误,即使将代码封装在超时为
块中也是如此


尽管Apple显然支持执行AppleScripts来响应消息事件,也许我应该看看其他机制来支持客户端的请求。我对想法持开放态度。

不知何故,我找到了一个简单但(非常)肮脏的黑客,似乎对我有效,但我不能说它是否能在任何机器上工作。因此,“收到的活动聊天信息”似乎同时被调用了两次,但我注意到类似于
do shell脚本“php-r'echo microtime()>>file.txt'”
有时显示的值略有不同。 我还使用属性作为标志,并尝试通过写入文件来利用shell执行的微小间隔:

echo 0 > ~/Documents/flag.txt
然后:

瞧。同样,我不能说这个解决方案是否每次都有效,而解释为什么它在我的案例中确实有效,这远远超出了我目前的能力。不过,我希望它会有用。干杯

property flag : 0
using terms from application "Messages"
    #...
    on active chat message received theMessage from theBuddy

        set response to false
        set the_script to "cat ~/Documents/flag.txt"
        set flag to do shell script the_script
        do shell script "echo 1 > ~/Documents/flag.txt" 
        if flag is "0" then
            set response to true
        else
            do shell script "echo 0 > ~/Documents/flag.txt"
        end if

        if response then
            #this should be executed only once
        end if    

    end active chat message received
    #...
end using terms from