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

标签 cocoa applescript

我正在尝试向 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>

仍然欢迎任何改进/提示/批评。

最佳答案

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

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

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

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

关于cocoa - 使 Cocoa 应用程序响应简单的 AppleScript 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004312/

相关文章:

applescript - 如何将包含多个项目的列表合并到 1 个列表中? (苹果脚本)

iphone - 如何在 cocoa 中获取有关 LAN 网络中设备的信息

cocoa - Mac OS X Window Server 与 X11 : the insane task

swift cocoa : Change NSScrollView frame size with animation

cocoa - 是否有适用于 Windows 的 Cocoa 框架或像 Mac 中那样的适用于 Windows 的界面构建器?

macos - 在 MacOSX 上禁用/取消 sleep 命令

ruby - 如何在 RubyCocoa 中激活(置于前面)?

swift - 将数据的排序列表保存到核心数据中

Applescript - 如何迭代轨道

file-upload - 帮助 AppleScript cURL 文件上传