试图从带有applescript的os x消息接收消息

Posted

技术标签:

【中文标题】试图从带有applescript的os x消息接收消息【英文标题】:Trying to receive a message from os x message w/ applescript 【发布时间】:2012-09-11 20:39:56 【问题描述】:

嘿,我正在运行以下脚本:

using terms from application "Messages"
    on message received this_message from this_buddy for this_chat
        display dialog "test"
    end message received
end using terms from

但是每次收到消息时,我都会在消息中收到以下错误:

Event: Message Received in Active Chat
File: registerToReceiveMessages.applescript
Error: Error -1708

我在互联网上的任何地方都找不到该错误。它似乎适用于除活动聊天之外的每个聊天。有什么想法吗?

此外,我正在尝试为“收到的地址消息”添加事件,但每次我编译 applescript 时都会将其替换为“收到的远程屏幕共享邀请”

【问题讨论】:

你有想过这个吗? 哈哈,同样的问题。 AppleScript,一种具有如此现代概念但带有错误消息的语言,让您想起 1980 年代! 错误提示“未处理的事件”Ref,但我找不到在活动聊天中收到消息的事件 【参考方案1】:

只要您选择了不处理指定事件的 AppleScript,您就会收到 Error -1708

例如,如果您的脚本只实现了on message sent,但您设置此脚本为每次接收消息时运行,您将收到错误-1708。这是因为您的脚本只知道如何处理传出消息,而不知道如何处理传入消息,因此出现错误 -1708。

现在有一些有趣的事情......

如果您尝试对事件Message ReceivedMessage Received in Active ChatMessage Sent 使用默认脚本Mix Message Case.applescript。第一个和最后一个工作正常,但您会收到一个 -1708 错误活动聊天事件。我们可以推断这意味着脚本没有处理事件Message Received in Active Chat。因此,现在看来,即使是 Apple 也无法处理此事件。

OS X Mavericks 更新:

此更新修复了前面提到的错误。如果您选择 Apple 的示例脚本 Speak Events.applescript,您会注意到它可以完美地处理接收到活动聊天室的消息。如果您检查代码,您会注意到它使用了on active chat message received 方法。我们现在可以在我们的脚本中使用它。由于我不再安装旧版本,因此我无法测试相同的方法在以前的版本中是否有效。

这是 Speak Events.applescript 中的代码:

on active chat message received with eventDescription
    say eventDescription
end active chat message received

还要注意您不再指定要为特定事件运行的单个脚本。相反,您为 Messages 事件指定单个脚本处理程序。这意味着您必须实现所有事件以避免获得 -1708 方法。请注意,在示例脚本中,Apple 甚至有注释 # The following are unused but need to be defined to avoid an error。这是一个可以用作脚本起点的模板:

using terms from application "Messages"
    # The following are unused but need to be defined to avoid an error

    on message sent theMessage with eventDescription
    end message sent

    on message received theMessage with eventDescription
    end message received

    on chat room message received with eventDescription
    end chat room message received

    on active chat message received with eventDescription
    end active chat message received

    on addressed message received theMessage from theBuddy for theChat with eventDescription
    end addressed message received

    on received text invitation with eventDescription
    end received text invitation

    on received audio invitation theText from theBuddy for theChat with eventDescription
    end received audio invitation

    on received video invitation theText from theBuddy for theChat with eventDescription
    end received video invitation

    on received local screen sharing invitation from theBuddy for theChat with eventDescription
    end received local screen sharing invitation

    on buddy authorization requested with eventDescription
    end buddy authorization requested

    on addressed chat room message received with eventDescription
    end addressed chat room message received

    on received remote screen sharing invitation with eventDescription
    end received remote screen sharing invitation

    on login finished with eventDescription
    end login finished

    on logout finished with eventDescription
    end logout finished

    on buddy became available with eventDescription
    end buddy became available

    on buddy became unavailable with eventDescription
    end buddy became unavailable

    on received file transfer invitation theFileTransfer with eventDescription
    end received file transfer invitation

    on av chat started with eventDescription
    end av chat started

    on av chat ended with eventDescription
    end av chat ended

    on completed file transfer with eventDescription
    end completed file transfer

end using terms from

如果您从这个脚本开始并只实现您需要的方法(同时保持其余部分不变),那么您应该避免所有 -1708 错误。

【讨论】:

【参考方案2】:

在我看来,message received 事件处理程序只有在 消息 中感觉像它时才起作用。我有一个稍微不同的脚本同样的问题。在另一个网站上找到另一个示例后,将其复制粘贴到新的 AppleScript 编辑器窗口中,并将其保存在由 Messages 复制到 ~/Library/Scripts/Messages 的 AppleScript 文件上,它开始工作。

您的脚本似乎没有问题。如果我用您的代码替换我当前的脚本并保存脚本,它会按预期工作,显示一个包含文本 test 的对话框。

将脚本设置为运行None 并返回到您在消息设置中创建的脚本也可能就足够了。

【讨论】:

以上是关于试图从带有applescript的os x消息接收消息的主要内容,如果未能解决你的问题,请参考以下文章

OS X (macOS) 和低级 C(Objective-C 替代方案)中的 Applescript

AppleScript从Mail.app上的Imap文件夹中选择消息

使用 AppleScript 发送消息

通过 AppleScript 从 iMessage 发送 GIF

从 Mac OS X 10.6 64 位 macbook 运行时出现 CORBA 异常

OS X Dock API?检索带有徽章和其他修改的 OS X 活动应用程序的图标