我正在尝试向我编写的Cocoa应用程序添加一些脚本功能。我为我的项目创建了一个sdef(脚本定义文件)。到目前为止,我已经成功地使用AppleScript访问对象子元素(元素),但是我不能在我的生活中弄清楚如何调用方法(命令)。在Cocoa应用程序中定义和执行简单的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>
这里是我的控制器类的实现(这是我的申请的代表)
MyController.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."
任何想法,回应我在做什么错在这里?我觉得我错过了一些简单的东西,但是我的谷歌搜索似乎没有提供任何有用的东西。
也许我错过了一些东西,但这是一个可可(OS X)的应用程序或可可触摸(iPhone)的应用程序? iPhone应用程序不支持AppleScript。 – sbooth 2009-07-24 01:34:08
提问者澄清说,UIController是自定义应用程序控制器/委托类的(选择不当)名称,所以我更新了代码以消除混淆。 – 2009-07-29 16:21:35