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應用程序中實現自定義腳本命令,如果是這樣,如何實現?
不要忘記在兩天內回來接受你自己的答案(http://blog.stackoverflow.com/2009/01/accept-your-own-answers/)。 –