使 Objective C 程序响应 applescript

发布于 2024-09-04 04:10:45 字数 292 浏览 7 评论 0原文

我有一个 Objective C 程序,我正在尝试添加脚本能力。我已经阅读了文档,并且看到了 SimpleScripting 示例。所以,我有两个问题:

1)我是否正确地说每个(非标准)命令都需要它自己的类来响应该命令?该类可能只有一种方法。

2)让该类与程序的其余部分进行交流的公认方式是什么?例如,如果我希望命令告诉我的应用程序进行保存,脚本对象如何知道要向其发送消息的对象?我可以看到让我的 NSApplication 类设置一个全局 gApplication = this,然后通过它执行所有操作,但这似乎有点混乱......

I have an Objective C program, and I'm trying to add scriptability. I have read the docs, and I've seen the SimpleScripting examples. so, I have two questions:

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.

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我三岁 2024-09-11 04:10:45

1)我是否正确地说每个(非标准)命令都需要它自己的类来响应该命令?该类可能只有一个方法。

不,您可以定义 对象第一个脚本命令。在脚本字典中,使用类中的 元素来声明它接受命令以及要调用的方法。该方法必须采用 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>

在某些标头中:

@interface Thing (Scripting)
-(NSString*)doIt:(NSScriptCommand *)command;

在脚本中调用时,会给出适当类型的对象作为第一个参数。

tell application "SomeApp"
    do it thing 1
end

2)让该类与程序的其余部分进行通信的可接受的方式是什么?例如,如果我希望命令告诉我的应用程序进行保存,脚本对象如何知道要向其发送消息的对象?我可以看到让我的 NSApplication 类设置一个全局 gApplication = this,然后通过它执行所有操作,但这似乎有点混乱......

动词优先脚本命令 (需要子类化 NSScriptCommand)应该得到通过调用 NSScriptCommand 的 evaluatedArguments 方法。您需要处理的任何对象都应该可以从参数访问。如果不是,那么你的设计就有问题。

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.

No. You can define object first script commands. In the scripting dictionary, use the <responds-to> element in a class to declare that it accepts the command, and which method to call. The method must take an 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>

In some header:

@interface Thing (Scripting)
-(NSString*)doIt:(NSScriptCommand *)command;

When called in a script, an object of the appropriate type is given as the first argument.

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 (which will require subclassing NSScriptCommand) should get passed the objects they need to work on by calling NSScriptCommand's evaluatedArguments method. Any objects you need to work on should be reachable from the arguments. If not, there's a problem with your design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文