How do you specify multiple AppleScript child elements of the same class?

谁说我不能喝 提交于 2019-12-13 16:02:02

问题


I'm adding Apple Event scripting to my application. I would like to be able to use both of the statements below on it:

-- Every instance of MyObject in the app
every MyObject

-- Only the instances of MyObject the user has selected
selected MyObjects

These are relevant excerpts from of my sdef file:

<dictionary>
    <suite ...>
        <class name="application" code="capp" description="Top-level scripting object" plural="applications" inherits="application">
            <cocoa class="MyAppClass" />

            <element type="MyObject" access="r">
                <cocoa key="myobjects" />
            </element>

            <element name="selected MyObjects" code="ABCD" type="MyObject" access="rw">
                <cocoa key="selectedMyObjects" />
            </element>
        </class>

        <class name="MyObject" code="ABcd" inherits="item" plural="MyObjects">
        ...
        </class>
    </suite>
</dictionary>

When I call every MyObject, it returns a list of objects, as expected. But when I call selected MyObjects, the Script Editor selects the "MyObjects" portion and complains:

Syntax Error

Expected end of line, etc. but found plural class name.

How can I achieve what I'm looking to do?


回答1:


Your design is wrong. See the Scripting Interface Guidelines for a somewhat thin but better-than-nothing outline of good UX practices.

The correct idiom is to define a selection property, typically on application and/or document. This may be read-write or read-only, depending on what's appropriate to your application.

The selection property's value is either:

  1. A single bespoke object specifier that identifies all of the currently selected objects e.g.:

    selection of application "Foo"
    

    or

    selection of document X of application "Foo"
    

    Some of the better Carbon-based apps and the occasional Cocoa app use this approach, which allows users to perform powerful queries such as:

    get (name of every MyObject of selection of document X)
    
    delete (every job whose status is completed)
    

    but requires more work for you to implement.

  2. A list of single-object specifiers, each identifying one selected item, e.g.:

    {thing B of document X of application "Foo", 
     thing E of document X of application "Foo",...}
    

    This is less powerful, since users cannot manipulate all of the selected items in a single command but must instead iterate the list processing each item one at a time, but is cheaper for you to implement. Cocoa Scripting being somewhat lame and inflexible even at the best of times, and pretty damn hopeless at moving/duplicating/deleting more than one object at a time, this is the most common approach in Cocoa-based apps.

    Update your sdef file with this property:

    <property name="selection" code="ABCD">
        <cocoa key="selectedMyObjects" />
        <type type="MyObject" list="yes" />
    </property>
    

For an example of the first (better) design approach, take a look at Script Editor's own dictionary. For an example of the second, see Mail's dictionary.

(Tip: To view a dictionary in Script Editor, choose File>Open Dictionary and select the appropriate item from the list. To export that dictionary as an SDEF file, just make sure the dictionary viewer window is frontmost and choose File>Save As.)



来源:https://stackoverflow.com/questions/28121604/how-do-you-specify-multiple-applescript-child-elements-of-the-same-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!