使Cocoa应用程序响应简单的AppleScript命令

Raú*_*che 6 cocoa applescript

我正在尝试为Cocoa应用程序添加一个简单的AppleScript支持.应用程序定期执行检查,我只是希望能够告诉它按需执行它.

我试图遵循SimpleScriptingVerbs Apple示例.

我的子类NSScriptCommand如下:

标题:

#import <Cocoa/Cocoa.h>

@interface rdrNotifierUpdateCommand : NSScriptCommand {

}
-(id)performDefaultImplementation;
@end
Run Code Online (Sandbox Code Playgroud)

执行:

#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
Run Code Online (Sandbox Code Playgroud)

我的.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>
Run Code Online (Sandbox Code Playgroud)

Info.plist被适当的设置.

但是,当我尝试在AppleScript编辑器中运行以下脚本时:

tell application "rdrNotifier"
    do update
end tell
Run Code Online (Sandbox Code Playgroud)

我收到一个关于变量"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>
Run Code Online (Sandbox Code Playgroud)

任何改进/提示/批评仍然是受欢迎的.

bre*_*ung 2

感谢您为将 applescript 支持添加到您的应用程序所做的努力!只是一个快速的观察/批评:在构建术语时,无论如何都要包含空格,但如果该术语采用“动词名词”的形式(例如“do update”),如果名词“update”不是正确的,AppleScripters 会感到恼火。建模对象,如果“do”不是正确的命令。

我认为“do”作为命令名称是一个糟糕的选择,因为它非常模糊。仅使用“update”作为命令有什么问题?(即将其视为动词)。

Apple 自己的技术说明 2106(当前位于此处)对此问题进行了一些详细处理,如果您希望取悦您的 AppleScripting 用户社区,您绝对应该阅读并消化该说明。

Apple 本身也没有超越愚蠢的术语选择,例如 updatePodcast 和 updateAllPodcasts(在 iTunes 中)。这些都是愚蠢的选择,因为(根据 tn2106)它们不包含空格,并且因为更好的选择是拥有一个适当的播客类,然后拥有一个可用于单个播客、所有播客或特定选择的更新命令一组播客。