我正在嘗試向我編寫的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