更新:如下所述,观察到的行为是一个错误。但是,OP 自己找到了一种解决方法:事件的from
参数message received
(theBuddy
在示例代码中)是Buddy
该类的一个实例,它也有一个service
属性,可以正常工作:
set recvService to name of service of theBuddy # instead of the buggy `of theChat`
注意:message received
事件的for
参数(theChat
在 OP 的代码中)是text chat
类的一个实例,它继chat
承自基类。如果脚本编辑器中的字典查看器未配置为显示继承text chat
的项目,则通过查看它确实具有 - 当前有问题的 -service
属性的类将不会很明显。要使继承的项目可见,请打开脚本编辑器的首选项对话框并选中Show inherited items
.
AppleScript 支持中似乎存在一个错误Messages.app
(在 OSX 10.11.1 上观察到);我建议您在http://bugreport.apple.com提交错误;这是一个简单的演示:
tell application "Messages"
set svc to first item of services
set cht to first item of (chats of svc)
# !! Breaks, even though it should return the same object as `svc`.
# "Can’t get service of text chat id \"iMessage;-;<recipient>\"."
service of cht
end tell
似乎尝试访问实例的任何属性当前已被破坏。text chat
您可以尝试一种解决方法:
# Given a chat instance, returns its originating service.
on getService(cht)
local svc, cht
tell application "Messages"
repeat with svc in services
try
(first chat of svc whose it is cht)
return contents of svc
end try
end repeat
end tell
return missing value
end getService
# Sample invocation
set recvService to name of (my getService(theChat))