gpt4 book ai didi

objective-c - 将记录类型返回到 applescript(来自 objective-C)

转载 作者:搜寻专家 更新时间:2023-10-30 19:49:09 24 4
gpt4 key购买 nike

我正在尝试将记录返回到 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}。"来自 {«class mess»:"", «class code»:0} 消息的编号 -1728。

最佳答案

您的 sdef 和实现都很好,但是您在 sdef 中定义的术语在 AppleScript tell block 之外不可用。你的脚本应该是:

tell application "LyX-2.1-qt4"
set x to (run "server-get-filename" with argument "")
message of x
end tell

或者,如果您出于某种原因确实需要 tell block 之外的消息:

set msg to missing value
tell application "LyX-2.1-qt4"
set x to (run "server-get-filename" with argument "")
set msg to message of x
end tell
log msg

关于objective-c - 将记录类型返回到 applescript(来自 objective-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22144649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com