让Cocoa应用程序简单的AppleScript响应命令 [英] Make Cocoa application respond to simple AppleScript command

查看:112
本文介绍了让Cocoa应用程序简单的AppleScript响应命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个简单的AppleScript支持加入到一个Cocoa应用程序。该应用程序会定期进行检查,我只是希望能够告诉它按需执行它。

I am trying to add a trivial AppleScript support to a Cocoa application. The application performs a check periodically and I just want to be able to tell it to perform it on demand.

我试图按照 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 文件去如下(和问题似乎是,但我不能找到它):

My .sdef file goes as follows (and the problem seems to be there, but I cannot find it):

<?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编辑器下面的脚本:

But, when I try to run the following script in AppleScript editor:

tell application "rdrNotifier"
    do update
end tell

我收到有关变量更新未被定义的错误。

I receive an error about variable "update" not being defined.

我可以打开字典从AppleScript的编辑器(即注册成功)我的申请。

I can open the dictionary for my application from AppleScript Editor (i.e. it is successfully registered).

编辑:找到一个解决办法

问题确实在 sdef 文件:我是不指定应用程序可以回复命令。我最后的定义去如下(对象 - code不变):

The problem was indeed in the sdef file: I was not specifying that the application can reply to the command. My final definition goes as follows (Obj-C code unchanged):

<?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>

任何改进/提示/批评仍然是值得欢迎的。

Any improvements/tips/criticisms are still welcome.

推荐答案

感谢您使您的应用程序添加AppleScript的支持的努力!只是一个快速的观察/批评:在构建术语,通过各种手段包括空格,但是如果这一术语采用动词名词的形式(如做更新')appleScripters会被激怒,如果名词更新是不是正确建模对象,如果做是不是一个正确的命令。

Thanks for making the effort of adding applescript support to your app! Just a quick observation / criticism: When constructing terminology, by all means include spaces, but if that terminology takes the form of 'verb noun' (like 'do update') appleScripters will be irritated if the noun 'update' is not a properly modelled object, and if 'do' is not a proper command.

我认为,做是一个命令的名字一个糟糕的选择,因为它是非常模糊的。这有什么错只使用'更新'的命令? (即把它当作一个动词)。

I would argue that 'do' is a poor choice for a command name, because it is very vague. What's wrong with just using 'update' as the command? (i.e. treat it as a verb).

这个问题在一些细节由苹果公司自己的技术说明2106(目前的这里),您一定要读,如果你希望讨好你的AppleScripting用户群体消化。

This issue is treated in some detail by Apple's own technote 2106 (currently here), which you definitely should read and digest if you hope to please your AppleScripting user community.

苹果自己都无法超越愚蠢的术语选择,如updatePodcast和updateAllPodcasts(在iTunes)。这是愚蠢的选择,因为(根据tn2106)它们不包含空格,因为更好的选择是将有一个适当的播客类,然后有可能对个人播客中使用更新命令,所有播客,或特定的选择设置播客。

Apple themselves are not beyond stupid terminology choices such as updatePodcast and updateAllPodcasts (in iTunes). These are stupid choices because (according to tn2106) they contain no spaces, and because the better choice would be to have a proper podcast class and then have an update command which could be used on an individual podcast, all podcasts, or a particular chosen set of podcasts.

这篇关于让Cocoa应用程序简单的AppleScript响应命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆