我正在尝试为Cocoa应用程序添加一个简单的AppleScript支持。该应用程序定期执行检查,我只想让它在需要时执行。
我正在尝试遵循SimpleScriptingVerbs苹果公司的示例。
我已经按照以下方式对NSScriptCommand
进行了子类化:
头文件:
#import <Cocoa/Cocoa.h>
@interface rdrNotifierUpdateCommand : NSScriptCommand {
}
-(id)performDefaultImplementation;
@end
实现:
#import "rdrNotifierUpdateCommand.h"
#import "rdrNotifierAppDelegate.h"
@implementation rdrNotifierUpdateCommand
-(id)performDefaultImplementation {
NSLog(@"Works at last");
[((rdrNotifierAppDelegate *)[[NSApplication sharedApplication] delegate])
checkForNewItems]; // This just fires the timer
return nil;
}
@end
我的.sdef文件如下所示(问题似乎就在那里,但我找不到它):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="Dictionary" xmlns:xi="http://www.w3.org/2003/XInclude">
<xi:include href="file:///System/Library/ScriptingDefinitions/CocoaStandard.sdef" xpointer="xpointer(/dictionary/suite)"/>
<suite name="rdrNotifier Suite" code="rdrN" description="rdrNotifier application specific scripting facilities.">
<command name="do update" code="rdrNUpdt" description="Check for new items">
<cocoa class="rdrNotifierUpdateCommand"/>
</command>
</suite>
</dictionary>
Info.plist
已经适当设置。
但是,当我尝试在AppleScript编辑器中运行以下脚本时:
tell application "rdrNotifier"
do update
end tell
我收到一个关于变量“update”未定义的错误。
我可以从AppleScript编辑器打开我的应用程序字典(即成功注册)。
编辑:找到了解决方案
问题确实在sdef文件中:我没有指定应用程序可以回复该命令。我的最终定义如下(Obj-C代码未更改):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="Dictionary" xmlns:xi="http://www.w3.org/2003/XInclude">
<!-- I have removed the standard suite as the application does not open, print... -->
<suite name="rdrNotifier Suite" code="rdrN" description="rdrNotifier application specific scripting facilities.">
<command name="do update" code="rdrNUpdt" description="Check for new items">
<cocoa class="rdrNotifierUpdateCommand"/>
</command>
<class name="application" code="Capp">
<cocoa class="NSApplication"/>
<responds-to name="do update">
<!-- Note you need to specify a method here,
although it is blank -->
<cocoa method=""/>
</responds-to>
</class>
</suite>
</dictionary>
任何改进/提示/批评仍然受欢迎。