Cocoa Scripting: Integrating the Text Suite

181 views Asked by At

I am attempting to use the Text Suite in my scriptable Mac app.

The little documentation I found in the Cocoa Scripting Guide suggests to use NSTextStorage. But it does not explain how the rest needs to be set up, both in the Sdef and on the coding side.

In particular, I wonder how I tell my scripting definition when to use the Text Suite with its richer commands and when not. The problem I see is that the Text Suite declares its own type named text. But that's already a predefined type used for plain text as well.

So I end up with two choices of "text" in the Sdef editor's text selector for my elements' properties, and to the Sdef that gets written it's the same type. Meaning I cannot make a distinction in the Sdef between properties that handle text that supports the Text Suite and those that don't, even if my code doesn't always use NSTextStorage for them.

Does that mean that I need to store all my scriptable properties in objects of class NSTextStorage? Otherwise, a user may expect to use the extended Text Suite commands on any textual property but will get runtime errors if I use NSString instead of NSTextStorage for them, right?

But for simple properties that only give simple plain text strings back, such as the app's name, it would be overkill to support the Text Suite, doesn't it?

So, how shall I go about this? Shall I simply let the user figure it out when he can use the Text Suite and where not because I use NSTextStorage only for properties that I deem worthy of the rich text support? How do others solve this?

Update

Turns out that when I simply add the Text Suite to my Sdef (I'm using the one the Sdef editor offers as "NSTextSuite" under its File submenu), all properties that return text stop working (causing error -10000). I've compared the Text Suite entries to those from other apps and cannot see any obvious differences.

Why would adding the Text Suite break all properties that have a property of type "text"?

What follows is an abbreviated Sdef so you can see what I'm doing:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="Demo Terminology">
    <suite name="Demo Suite" code="Demo" description="Demo suite">
        <class name="application" code="capp" description="The application&apos;s top-level scripting object.">
            <cocoa class="NSApplication"/>
            <property name="demo text" code="DeTx" description="A text prop for testing" type="text">
                <cocoa key="testText"/>
            </property>
        </class>
    </suite>
    <suite name="Text Suite" code="????" description="A set of basic classes for text processing.">
        <cocoa name="NSTextSuite"/>
        <class name="text" code="ctxt" description="Rich (styled) text" plural="text">
            <cocoa class="NSTextStorage"/>
            <element type="paragraph">
                <cocoa key="paragraphs"/>
            </element>
            <element type="word">
                <cocoa key="words"/>
            </element>
            <element type="character">
                <cocoa key="characters"/>
            </element>
            <element type="attribute run">
                <cocoa key="attributeRuns"/>
            </element>
            <element type="attachment">
                <cocoa key="attachments"/>
            </element>
            <property name="color" code="colr" description="The color of the first character." type="color">
                <cocoa key="foregroundColor"/>
            </property>
            <property name="font" code="font" description="The name of the font of the first character." type="text">
                <cocoa key="fontName"/>
            </property>
            <property name="size" code="ptsz" description="The size in points of the first character." type="number">
                <cocoa key="fontSize"/>
            </property>
        </class>
        <class name="attachment" code="atts" description="Represents an inline text attachment.  This class is used mainly for make commands." inherits="text">
            <cocoa class="NSAttachmentTextStorage"/>
            <!-- This property should be deprecated like all the other path-centric properties, and replaced with a type="file" property. -->
            <property name="file name" code="atfn" description="The path to the file for the attachment" type="text">
                <cocoa key="filename"/>
            </property>
        </class>
        <class name="paragraph" code="cpar" description="This subdivides the text into paragraphs." inherits="text">
            <cocoa class="NSTextStorage"/>
        </class>
        <class name="word" code="cwor" description="This subdivides the text into words." inherits="text">
            <cocoa class="NSTextStorage"/>
        </class>
        <class name="character" code="cha " description="This subdivides the text into characters." inherits="text">
            <cocoa class="NSTextStorage"/>
        </class>
        <class name="attribute run" code="catr" description="This subdivides the text into chunks that all have the same attributes." inherits="text">
            <cocoa class="NSTextStorage"/>
        </class>
    </suite>
    <suite name="Standard Suite" code="????" description="Common classes and commands for most applications.">
        <cocoa name="NSCoreSuite"/>
        <class name="color" code="colr" description="A color.">
            <cocoa class="NSColor"/>
        </class>
    </suite>
</dictionary>

Running this script will then result in error -10000:

tell application "Demo"
    demo text
end tell

If I remove the "Text Suite" from the Sdef, the code runs without error.

2

There are 2 answers

0
Thomas Tempelmann On

Looks like the Text Suite that the Sdef Editor offers is not usable for apps using the Cocoa Scripting framework. Nor can one adapt the technique that "TextEdit.app" uses, where the text class gets an id assigned ("text.ctxt"), which is then used with property types that shall use the Text Suite's text class.

Instead, the one from the "Sketch" sample code should be used. That one renames the type from "text" to "rich text", thereby solving the issue of conflicting type names I have described in my question.

I find it curious that that "classic" apps use the type "text" for both plain and rich text properties, whereas that appears not possible any more with modern apps that rely on the new Cocoa Scripting framework.

2
Ron Reuter On

I believe there is an error in the generated SDEF in the line:

    <cocoa name="NSTextSuite" />

The tag is used to reference classes or variables in your code. There is no class named NSTextSuite in the framework, afaik. The text suite definition should start like this:

<suite name="Text Suite" code="????" description="...">
    <class name="rich text" plural="rich text" code="ctxt" description="Rich (styled) text." inherits="item" hidden="yes" >
        <cocoa class="NSTextStorage"/>
        <type type="text"/>
        ...