我正在尝试向我编写的 Cocoa 应用程序添加一些脚本功能。我为我的项目创建了一个sdef(脚本定义文件)。到目前为止,我已经成功地使用 AppleScript 访问对象子项(元素),但我一生都无法弄清楚如何调用方法(命令)。
这是我的 sdef 文件。
<suite name="mySuite" code="mSUI" description="My Application Suite">
<class name="application" code="capp" description="Top level scripting object.">
<cocoa class="NSApplication"/>
<!-- I can access these elements fine -->
<element description="Test children." type="child" access="r">
<cocoa key="myChildren"/>
</element>
<!-- Cannot seem to call this method :( -->
<responds-to command="testmethod">
<cocoa method="exposedMethod:"/>
</responds-to>
</class>
<class name="child" code="cHIL" description="A Child." plural="children">
<cocoa class="Child"/>
<property name="name" code="pnam" description="The child name." type="text" access="r">
<cocoa key="name"/>
</property>
</class>
<command name="testmethod" code="tEST" description="Execute the test method" />
</suite>
这是我的控制器类实现(这是我的应用程序的委托)
我的控制器.h
#import <Cocoa/Cocoa.h>
@interface MyController : NSObject {
NSMutableArray* myChildren;
}
// Some Methods
@end
MyController+Scripting.m
#import "MyController+Scripting.h"
@implementation MyController (Scripting)
// This works when I'm accessing the myChildren
- (BOOL)application:(NSApplication*)sender delegateHandlesKey:(NSString*)key {
NSLog(@"Key = %@", key);
return ([key isEqualToString:@"myChildren"]);
}
// This does NOT work...
- (void)exposedMethod:(NSScriptCommand*)command {
NSLog(@"Invoked Test Script Method %@", [command description]);
}
@end
最后,我正在尝试的 AppleScript 是:
tell application "MyApplication"
testmethod
end tell
回应"AppleScript Error - The variable testmethod is not defined."
有什么想法我在这里做错了吗?我觉得我错过了一些简单的东西,但我的谷歌搜索似乎没有找到任何有用的东西。