2011-05-14 73 views
6

我想为Cocoa应用程序添加一个简单的AppleScript支持。应用程序定期执行检查,我只是想能够告诉它按需执行。使可可应用程序响应简单的AppleScript命令

我正在尝试关注SimpleScriptingVerbs Apple示例。

我已经子类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 

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

我可以从AppleScript Editor打开我的应用程序的字典(即它已成功注册)。

编辑:找到一个解决办法

的问题确实在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> 

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

回答

2

感谢您为app增加applescript支持的努力!只是一个快速的观察/批评:当构建术语时,通过一切手段包括空格,但如果该术语采用'动词名词'的形式(如'更新'),如果名词'update'不正确,appleScripters将会被激怒建模对象,如果'做'不是一个适当的命令。

我会认为'做'是命令名称的糟糕选择,因为它非常含糊。使用'update'作为命令有什么问题? (即将其视为动词)。

此问题由Apple自己的technote 2106(当前为here)进行了详细处理,如果您希望取悦AppleScripting用户社区,那么您绝对应该阅读并摘录此问题。

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

+0

非常感谢您指点我的技术。是的,“更新”听起来很愚蠢。把“做”(我的意思是“执行”,但使用它也无济于事)没有任何意义。我已将代码更改为单个动词,实际上也抛出了“更新”,并以“刷新”结束。 – 2011-06-06 14:27:38

+0

刷新听起来很棒! :)非常令人耳目一新 – brennanyoung 2011-06-06 18:09:50

+0

我看不出这是甚么远程答案。 – Volomike 2016-04-16 22:28:40

相关问题