Applescript command in a document class

I am trying to create an applescript command in my document class. I know that I am doing something wrong, but I am not sure how to do it.

From what I understand (which may be wrong) when I create a new team I need to specify a new class for this command. But from this new class, let's call him ScriptResetCommand, how do I access a document object from a method performDefaultImplementation? Calling applescript is like

tell document 1 of application "DocScript" to simple reset

Here is my current code: ScriptResetCommand.m file:

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

File ScriptResetCommand.h:

@interface ScriptResetCommand : NSScriptCommand
- (id)performDefaultImplementation;

.sdef file:  

    <command name="simple reset" code="jDsgSrst" description="run a simple reset">
        <cocoa class="ScriptResetCommand"/>
        <result type="text" description="returns the result"/>
    </command>

So, with this code, I can successfully call the method performDefaultImplementationin the class ScriptResetCommand, but how can I access the object Documentthat has the desired command in it reset?

Thanks in advance.

+4
2

-[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";
}
0

,

tell document 1 of application "DocScript" to simple reset

( reset) ( 1 "DocScript" ). , [command directParameter], objectSpecifier .

, , Ted. () "select", " 5 1".

tell application "Ted"
    select paragraph 5 of document 1
end tell

SDEF: Text Suite:

<suite name="Text Suite" code="????">
    ...
    <class name="paragraph" code="cpar" inherits="item" >
        ...
        <responds-to command="select">
            <cocoa method="handleSelectCommand:"/>
        </responds-to>
    </class>
    ...
</suite>

"select" EVERY , : . , , , .

:

, , NSTextStorage, NSTextStorage .

@implementation NSTextStorage (Scriptability)
-   (void) handleSelectCommand:(NSScriptCommand *)command
{
    NSScriptObjectSpecifier *directParameter =[command directParameter];

    NSScriptObjectSpecifier *container = [directParameter containerSpecifier];

    TedDocument *document = [self extractDocument:container];

    NSString *contents = document.textView.string;
    NSRange selRange;

    [self getRangeForSelectCommand:directParameter contents:contents range:&selRange];

    [document.textView setSelectedRange:selRange];
}
@end

directParameter, , " 5 1 " ", . :

// , .

- (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];
}
0

Source: https://habr.com/ru/post/1540000/


All Articles