【发布时间】:2014-03-03 10:46:23
【问题描述】:
我正在尝试将记录返回给 applescript。当前的解决方案是返回一个 NSDictionary 并在 sdef 文件中定义一个记录类型(Lyx 返回值),但这不起作用 - 我应该构造一个特定的 AppleScript 对象吗?
这是 sdef 文件
<!-- our special scripting suite for this example -->
<suite name="Lyx" code="LYX " description="LyX scripting facilities.">
<record-type name="LyX return value" code="LyxR">
<property name="code" code="code" type="integer"
description="Error code (0 in case of success).">
<cocoa key="code"/>
</property>
<property name="message" code="mess" type="text"
description="The returned message.">
<cocoa key="message"/>
</property>
</record-type>
<command name="run" code="SLyxComm" description="run a simple command with one parameter">
<cocoa class="LyxCommand"/>
<direct-parameter description="The command to be executed.">
<type type="text" list="no"/>
</direct-parameter>
<parameter name="with argument" code="args" type="text">
<cocoa key="arg"/>
</parameter>
<result type="LyX return value" description="Contains a code (0 for success) and the message returned by LyX"/>
</command>
</suite>
这里是 Objective-C 代码
@interface LyxCommand : NSScriptCommand {
}
- (id)performDefaultImplementation;
@end
@implementation LyxCommand
- (id)performDefaultImplementation {
// Get the command and argument
NSDictionary * theArguments = [self evaluatedArguments];
NSString * directParameter = [self directParameter];
NSString *arg = [theArguments objectForKey: @"arg"];
// Execute the command
LyXFunctionResult result = applescript_execute_command([directParameter UTF8String], [arg UTF8String]);
// Construct the result record
NSString *message = [NSString stringWithCString:result.message encoding:NSUTF8StringEncoding];
free(result.message);
NSDictionary *objcResult = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:result.code], @"code", message, @"message", nil];
return objcResult;
}
@end
当我尝试这个简单的 aplescript 时
tell application "LyX-2.1-qt4"
set x to (run "server-get-filename" with argument "")
end tell
message of x
错误信息是:error "Can’t get message of {message:\"\", code:0}." number -1728 from message of {«class mess»:"", «class code»:0}。
【问题讨论】:
-
使用
code of x或get (message of x),同样的错误:error "Can’t get code of {message:\"\", code:0}." number -1728 from code of {«class mess»:"", «class code»:0}。 -
顺便说一句,你不应该为你的自定义名称使用全小写的代码,因为它们是由 Apple 保留的,并且可能与你的冲突,意味着不同的东西。比如你在哪里使用 code="mess" 和 code="code"。
-
也许我对类似问题的回答之一可能对 oyu 有帮助。试一试(喜欢的话别忘了点赞):stackoverflow.com/a/37327603/15809
标签: objective-c applescript cocoa-scripting