Pergunta

EDIT: I've solved this; it was a configuration issue. Make sure your .sdef is copied/deployed with your app or it won't be found at runtime.

I'm building a Mac application using Mono and Monobjc and I'd like to be able to send it commands via AppleScript. I've read the Apple documentation and gotten their Simple Scripting Verbs example to work using Objective C, but I can't seem to translate it to work with Monobjc. Built-in commands like "quit" work, but the custom suite with a command that I add in the scripting definition works in the Objective C version but not in the mono version. The scripting definition being used is 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>

The working Objective C implementation for this command is this:

@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

My attempt to port this to Monobjc is this:

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);
        }
    }

}

When I run the Applescript command

tell application "TheObjCVersion"
    do simple command
end tell
  • When I try that with the Objective-C version, it returns 7, as you'd expect.
  • When I try that with the Mono version, it complains of a compiler error, flagging the "simple" in that command saying "xpected end of line but found identifier."

Is it possible to implement custom scripting commands in a Mono application, and if so, how?

Foi útil?

Solução

The problem was not with the .sdef or with my code, but rather with my project configuration. I failed to set the .sdef to be copied, so at runtime it wasn't found.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top