这个 Applescript 有什么问题吗?
我的应用程序具有一些基本的苹果脚本能力。有一种方法(receivedInstantMessage)采用单个参数(消息)并将其传递给我的应用程序,然后由我的应用程序处理它。
以下 applescript:
tell application "MyApp"
receivedInstantMessage "This is a message"
end tell
完美运行。我的应用程序显示一个包含消息的对话框(“这是一条消息”)。
我正在尝试对其进行设置,以便当我向 iChat 发送 IM 时,它会运行一个 applescript,将消息内容发送到我的应用程序。我已经告诉 iChat 在收到消息时运行脚本,并且我知道该部分正在工作。我现在使用的脚本不起作用:
using terms from application "iChat"
on message received theMessage from theBuddy for theChat
tell application "MyApp"
receivedInstantMessage theMessage
end tell
end message received
end using terms from
当我收到消息时没有任何反应。即使我替换 iChat 中的消息变量 (theMessage) 并使用任意字符串,它仍然不会执行任何操作。
我做错了什么。我对 applescript 很陌生(通常是 REALbasic 编码员)。
[更新]:这似乎现在有效。简单地重新启动 Mac 就可以解决问题。很奇怪...
My app has some rudimentary applescriptability. There is one method (receivedInstantMessage) that takes a single parameter (message) and passes it to my app which then processes it.
The following applescript:
tell application "MyApp"
receivedInstantMessage "This is a message"
end tell
Works perfectly. My app presents a dialog containing the message ("This is a message").
I'm trying to set it up so that when I send an IM to iChat, it runs an applescript that will send the contents of the message to my app. I have told iChat to run a script when a message is received and I know that part is working. The script I am now using does not work:
using terms from application "iChat"
on message received theMessage from theBuddy for theChat
tell application "MyApp"
receivedInstantMessage theMessage
end tell
end message received
end using terms from
Nothing happens when I receive a message. Even if I substitute the message variable (theMessage) from iChat and use an arbitrary string it still does nothing.
What am I doing wrong. I'm quite new to applescript (being a REALbasic coder normally).
[Update]: This seems to work now. A simple restart of the Mac fixed things. Very odd...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是语义细节:考虑脚本编写者正在向您的应用程序发送消息,而不是接收消息。是的,您的应用程序正在接收它们,但您选择的术语“receivedInstantMessage”是从您的应用程序的角度来看的,而不是脚本编写者的角度。
此外,使用驼峰式命名法被认为是无稽之谈。 AppleScript 术语可以(并且通常应该)包含空格。如果你真的想正确地做到这一点,你应该将术语分为名词和动词。 (其中名词是具有属性的正确建模的对象,动词是操作它们的命令。在这种情况下,您可能想要发送消息“bla”之类的东西,其中消息是具有发送者、接收者、通道等属性的对象。 send 是一个命令,它将消息对象作为参数 - 检查 Snak 字典是否有一个相当不错的 - 但不完美 - 实现)。
抱歉,如果这听起来像肛门。我已经从事 applescript 多年了,虽然当开发人员添加 applescript 支持时我真的很感激,但我知道当我说构建糟糕的字典和糟糕的术语选择令人沮丧和恼怒时,我代表所有 applescript 人员,特别是当应用程序变得更加成熟并且开发人员开始说“我知道 applescript 界面需要彻底检修但我不想破坏现有脚本!”之类的话。如果脚本界面变得更好,所有苹果脚本编写者都会更喜欢它,即使它会破坏现有脚本。所以:现在做错了,但准备好以后从根本上改进它。 :)
甚至 Apple 也有一些糟糕的术语,例如 iTunes 中有 updatePodcast 和 updateAllPodcasts 命令。根据他们自己的 technote 2106,这是错误的 -特别注意命名规则部分。它们应该有一个播客对象和一个更新命令,这样您还可以执行诸如“删除名称包含“Ann Coulter”的每个播客之类的操作。(“Whose”子句是以下之一) appleScript 最酷的功能!)
just a semantic detail: Consider the scripter is sending messages to your app, not receiving them. Yes, your app is receiving them, but the terminology you chose "receivedInstantMessage" is from the point of view of your app, not the scripter.
Additionally, it is considered naff to have terminology in camel case. AppleScript terminology can (and often should) contain spaces. And if you really want to do it properly you should separate the terminology into nouns and verbs. (Where nouns are properly-modelled objects, with properties, and verbs are commands for manipulating them. In this case you probably want something like send message "bla", where a message is an object with properties like sender, recipient, channel etc. and send is a command which takes a message object as a parameter - check the dictionary of Snak for a quite nice - but not perfect - implementation).
Sorry if this sounds anal. I have been applescripting for many years, and while I really appreciate it when developers add applescript support, I know I speak for all applescripters when I say that poorly constructed dictionaries, and poor terminology choices are frustrating and irritating, especially as the app gets more mature and the developer starts to say things like "I know the applescript interface needs a complete overhaul but I don't want to break existing scripts!". ALL applescripters prefer it if the scripting interface gets better, even if it breaks existing scripts. So: Do it wrong now, but be prepared to fundamentally improve it later. :)
Even Apple has some lousy terminology, for example in iTunes there is an updatePodcast and updateAllPodcasts command. This is just wrong, according to their own technote 2106 - pay particular attention to the section on naming rules.. They should have a podcast object and an update command, so that you could also do things like "delete every podcast whose name contains "Ann Coulter". ("Whose" clauses are one of the coolest features of appleScript!)