Objective c 文档类中的Applescript命令

Objective c 文档类中的Applescript命令,objective-c,macos,cocoa,applescript,Objective C,Macos,Cocoa,Applescript,我正在尝试在文档类中创建applescript命令。我知道我做错了什么,但我不知道该怎么做 根据我的理解(可能不正确),当我创建一个新命令时 我需要为该命令指定一个新类。但是从这个新的类,让我们称之为 ScriptResetCommand,如何从performDefaultImplementation方法访问文档对象?applescript调用类似于 tell document 1 of application "DocScript" to simple reset 这是我目前的代码: Scr

我正在尝试在文档类中创建applescript命令。我知道我做错了什么,但我不知道该怎么做

根据我的理解(可能不正确),当我创建一个新命令时 我需要为该命令指定一个新类。但是从这个新的类,让我们称之为
ScriptResetCommand
,如何从
performDefaultImplementation
方法访问文档对象?applescript调用类似于

tell document 1 of application "DocScript" to simple reset
这是我目前的代码: ScriptResetCommand.m文件:

@implementation ScriptResetCommand
- (id)performDefaultImplementation {
    // Somehow I need to access the correct document class and
    // perform my reset.
    NSLog(@"ScriptResetCommand performDefaultImplementation");
    return @"Reset Stuff";
}
@interface ScriptResetCommand : NSScriptCommand
- (id)performDefaultImplementation;
ScriptResetCommand.h文件:

@implementation ScriptResetCommand
- (id)performDefaultImplementation {
    // Somehow I need to access the correct document class and
    // perform my reset.
    NSLog(@"ScriptResetCommand performDefaultImplementation");
    return @"Reset Stuff";
}
@interface ScriptResetCommand : NSScriptCommand
- (id)performDefaultImplementation;
.sdef文件:


因此,有了这段代码,我可以在
ScriptResetCommand
类中成功调用
performDefaultImplementation
方法,但是如何访问
文档
对象,其中包含所需的
reset
命令


提前感谢。

您应该能够使用
-[NSScriptCommand evaluatedReceivers]
方法访问文档对象。试着这样做:

- (id)performDefaultImplementation {
    NSDocument *document = [self.evaluatedReceivers firstObject];
    if (![document isKindOfClass:[NSDocument class]]) {
        // I'm just guessing how this error should be handled; untested (for example, you'll want to make sure your app returns an error if you run "reset stuff" without specifying a document)
        [self setScriptErrorExpectedTypeDescriptor:[NSAppleEventDescriptor descriptorWithTypeCode:cDocument]];
        return nil;
    }

    NSLog(@"Got document: %@", document);

    return @"Reset Stuff";
}
我不知道是什么

tell document 1 of application "DocScript" to simple reset
可以,但要处理它,请将其分解为动词(简单重置)和目标(应用程序“DocScript”的文档1)。执行命令时,[command directParameter]中将提供对目标的引用,您可以从中获取文档的objectSpecifier

下面是我如何在我的脚本文本编辑器Ted中实现文本选择。动词(命令)为“选择”,目标为“文件1第5段”

SDEF:该命令在文本套件对象中定义:

<suite name="Text Suite" code="????">
    ...
    <class name="paragraph" code="cpar" inherits="item" >
        ...
        <responds-to command="select">
            <cocoa method="handleSelectCommand:"/>
        </responds-to>
    </class>
    ...
</suite>
对文档的引用由directParameter提供,但它是包含层次结构的一部分,在本例中为应用程序“Ted”的文档1的第5段,因此我们需要一种方法遍历该层次结构以从中提取文档。以下是该函数:

//如有必要,使用递归导航包含层次结构

- (TedDocument *) extractDocument:(NSScriptObjectSpecifier *)container
{
    if (container == nil)
    {
        NSLog(@"%@", @"ERROR: extractDocument received nil container reference");

        return nil;
    }

    FourCharCode containerClass =  [[container keyClassDescription] appleEventCode];

    if (containerClass != 'docu')
    {
        return [self extractDocument:[container containerSpecifier]];
    }

    return [container objectsByEvaluatingSpecifier];
}

谢谢你的建议。出于某种原因,上面的文档对象是nil。我还尝试了
NSDocument*doc2=[self-directParameter]
也返回nil。有趣的是,您是否尝试过通过打印
self进行调试。接收方指定人、自评参数和自评参数?其中一个可能有一些有趣的东西。看见
- (TedDocument *) extractDocument:(NSScriptObjectSpecifier *)container
{
    if (container == nil)
    {
        NSLog(@"%@", @"ERROR: extractDocument received nil container reference");

        return nil;
    }

    FourCharCode containerClass =  [[container keyClassDescription] appleEventCode];

    if (containerClass != 'docu')
    {
        return [self extractDocument:[container containerSpecifier]];
    }

    return [container objectsByEvaluatingSpecifier];
}