支持Mono应用程序中的Applescript



编辑:我已经解决了这个问题;这是一个配置问题。请确保你的.sdef已与你的应用程序一起复制/部署,否则在运行时找不到它

我正在使用Mono和Monobjc构建一个Mac应用程序,我希望能够通过AppleScript向它发送命令。我已经阅读了苹果的文档,并获得了使用Objective C的Simple Scripting Verbs示例,但我似乎无法将其翻译为使用Monobjc。像"退出"这样的内置命令有效,但我在脚本定义中添加的带有命令的自定义套件在Objective C版本中有效,但在mono版本中无效。正在使用的脚本定义是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,正如您所期望的那样
  • 当我在Mono版本中尝试这种方法时,它会抱怨编译器错误,在该命令中标记"simple",表示"expected end of line but found identifier"

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

问题不在于.sdef或我的代码,而在于我的项目配置。我未能将.sdf设置为要复制,所以在运行时找不到它。

相关内容

最新更新