在Mono应用程序中支持Applescripting

时间:2013-07-08 00:44:15

标签: mono applescript monobjc

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

我正在使用Mono和Monobjc构建Mac应用程序,我希望能够通过AppleScript发送命令。我已经阅读了Apple文档,并使用Objective C获得了他们的Simple Scripting Verbs示例,但我似乎无法将其翻译为使用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,正如您所期望的那样。
  • 当我尝试使用Mono版本时,它会抱怨编译器错误,在该命令中标记“简单”,说明“xpected end of line但找到了标识符。”

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

1 个答案:

答案 0 :(得分:1)

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