虽然Manu G E问了两次similar question,但都没有得到足够的答案,我希望我会有更好的运气。
当Messages.app收到一条消息时,我正在编写一个AppleScript来执行一个处理程序。该脚本将保存到~/Library/Application\ Scripts/com.apple.iChat,并在Messages首选项中设置为AppleScript处理程序。
当Messages是最前面的应用程序并且收到一条消息时,active chat message received处理程序将触发两次。当消息在后台时,这似乎不是问题(收到消息后触发message received,该处理程序只有一次)。我知道哪个处理程序被触发,因为处理处理程序的部分如下所示:
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,可以看到哪个处理程序被触发。
我尝试通过编写一个临时文件(使用_sender的UUID )来解决这个问题。message_received处理程序检查该文件是否存在,如果存在,则不执行任何操作。但这并不起作用,即使是随机延迟。我尝试延长随机延迟的长度,但这带来了有关AppleScript运行超过10秒的错误,即使在将代码封装在with timeout of块中时也是如此。
不管苹果明显支持执行AppleScripts来响应消息事件,也许我应该考虑一些其他机制来支持来自客户端的这个请求。我乐于接受各种想法。
发布于 2016-05-23 07:55:55
不知何故,我设法找到了一个简单但(非常)肮脏的黑客似乎对我有效,但我不能说它是否可以在任何机器上工作。所以"active chat message received“似乎会同时被调用两次,但我注意到像do shell script "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然后就这样。再说一次,我不能说这个解决方案是否每次都有效,并且解释为什么它在我的情况下实际上有效,这远远超出了我现在的能力。尽管如此,我希望它将是有用的。干杯
https://stackoverflow.com/questions/34800851
复制相似问题