开发者

making an Objective C program respond to applescript

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开发者_JS百科 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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜