2013-07-08 70 views
1

编辑:我解决了这个问题;这是一个配置问题。确保你的.sdef被你的应用程序复制/部署,否则它将不会在运行时找到。在Mono应用程序中支持Applescripting

我使用Mono和Monobjc构建Mac应用程序,我希望能够通过AppleScript发送命令。我已阅读Apple文档,并获得了他们的简单脚本动词示例以使用Objective C,但似乎无法将其转化为与Monobjc一起使用。诸如“退出”之类的内置命令可以工作,但带有在脚本定义中添加的命令的自定义套件适用于Objective C版本,但不适用于单声道版本。正在使用的脚本定义是Stest.sdef:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd"> 
<!-- declare the namespace for using XInclude so we can include the standard suite --> 

<dictionary xmlns:xi="http://www.w3.org/2003/XInclude"> 
    <!-- use XInclude to include the standard suite --> 
<xi:include href="file:///System/Library/ScriptingDefinitions/CocoaStandard.sdef" xpointer="xpointer(/dictionary/suite)"/> 
    <!-- specific suite(s) for the application follow... --> 
<suite name="Simple Scripting Verbs" code="SVrb" description="Terminology for the SimpleScriptingVerbs Sample."> 

    <command name="do simple command" code="SVrbSimp" description="run a simple command with no parameters"> 
     <cocoa class="SimpleCommand"/> 
     <result type="integer" description="returns the number seven"/> 
    </command> 

</suite> 

</dictionary> 

此命令的工作目标C实现是这样的:

@implementation SimpleCommand 


/* This class implements a simple verb with no parameters. The verb 
returns an integer number. Verbs don't get much simpler than this. */ 


- (id)performDefaultImplementation { 
SLOG(@"SimpleCommand performDefaultImplementation"); 

    /* return 7 to show how to return a number from a command */ 
return [NSNumber numberWithInt:7]; 
} 

@end 

我试图端口这Monobjc是这样的:

using System; 
using Monobjc; 
using Monobjc.AppKit; 
using Monobjc.Foundation; 

namespace STest 
{ 

    [ObjectiveCClass] 
    public class SimpleCommand : NSScriptCommand 
    { 
     public SimpleCommand() 
     { 
     } 

     public SimpleCommand(IntPtr nativePointer) : base(nativePointer) 
     { 
     } 

     [ObjectiveCMessage("performDefaultImplementation")] 
     public NSNumber performDefaultImplementation() 
     { 

      return NSNumber.NumberWithInteger (7); 
     } 
    } 

} 

当我运行Applescript命令时

tell application "TheObjCVersion" 
    do simple command 
end tell 
  • 当我尝试使用Objective-C版本时,它会返回7,正如您所期望的那样。
  • 当我尝试使用单声道版本时,它抱怨编译器错误,在该命令中标记“简单”,表示“xpected end of line but found identifier。”。

是否可以在Mono应用程序中实现自定义脚本命令,如果是这样,如何实现?

回答

1

问题不在于.sdef或与我的代码,而是与我的项目配置。我没有设置复制的.sdef,所以在运行时没有找到。

+1

不要忘记在两天内回来接受你自己的答案(http://blog.stackoverflow.com/2009/01/accept-your-own-answers/)。 –