我有一个 Objective C 程序,我正在尝试添加脚本能力。我已经阅读了文档,并且看到了 SimpleScripting 示例。所以,我有两个问题:
1)我是否正确地说每个(非标准)命令都需要它自己的类来响应该命令?该类可能只有一个方法。
2)让该类与程序的其余部分进行通信的可接受的方式是什么?例如,如果我希望命令告诉我的应用程序进行保存,脚本对象如何知道要向其发送消息的对象?我可以看到让我的 NSApplication 类设置一个全局 gApplication = this,然后通过它执行所有操作,但这似乎有点困惑......
最佳答案
1) Am I correct in saying that every (non-standard) command is going to need it's own class to respond to that command? And that class will probably only have one method.
没有。您可以定义 object first script commands 。在脚本字典中,使用<responds-to>
类中的元素来声明它接受命令以及调用哪个方法。该方法必须采用 NSScriptCommand*
.
<class name="thing" code="tHNG" description="Something" plural="things">
<cocoa class="Thing" />
<responds-to name="do it">
<cocoa method="doIt:" />
</responds-to>
</class>
<command name="do it" code="You DOIT" description="Do it, whatever that happens to be.">
<direct-parameter type="thing" description="A thing." />
<result description="it's done" type="text" />
</command>
在某些 header 中:
@interface Thing (Scripting)
-(NSString*)doIt:(NSScriptCommand *)command;
在脚本中调用时,会给出适当类型的对象作为第一个参数。
tell application "SomeApp"
do it thing 1
end
2) What is the accepted way of having that class communicate with the rest of the program? if I want the command to tell my application to save, for example, how does the script object know about the object to send messages to? I can see making my NSApplication class set a global gApplication = this, and then do everything through that, but that seems kind of kludgy...
Verb-first script commands (这将需要子类化 NSScriptCommand)应该通过调用 NSScriptCommand
来传递它们需要处理的对象。的 evaluatedArguments
方法。您需要处理的任何对象都应该可以从参数访问。如果不是,那么您的设计就有问题。
关于objective-c - 使 Objective C 程序响应 applescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2961726/