如何指定同一个类的多个 AppleScript 子元素? [英] How do you specify multiple AppleScript child elements of the same class?
问题描述
我正在向我的应用程序添加 Apple 事件脚本.我希望能够在其上使用以下两个语句:
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
这些是来自我的 sdef
文件的相关摘录:
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>
当我调用 每个 MyObject
时,它会按预期返回一个对象列表.但是当我调用 selected MyObjects
时,脚本编辑器选择了MyObjects"部分并抱怨:
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:
语法错误
预期的行尾等,但发现类名复数.
Expected end of line, etc. but found plural class name.
我怎样才能实现我想要做的事?
How can I achieve what I'm looking to do?
推荐答案
你的设计是错误的.请参阅脚本界面指南,了解略薄但优于- 没有关于良好用户体验实践的概述.
Your design is wrong. See the Scripting Interface Guidelines for a somewhat thin but better-than-nothing outline of good UX practices.
正确的习惯用法是定义一个selection
属性,通常在application
和/或document
上.这可能是读写或只读的,具体取决于适合您的应用程序的内容.
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.
selection
属性的值是:
一个单独的定制对象说明符,用于标识当前选择的所有对象,例如:
A single bespoke object specifier that identifies all of the currently selected objects e.g.:
selection of application "Foo"
或
selection of document X of application "Foo"
一些更好的基于 Carbon 的应用程序和偶尔的 Cocoa 应用程序使用这种方法,它允许用户执行强大的查询,例如:
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.
单对象说明符列表,每个说明符标识一个选定的项目,例如:
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",...}
这不是很强大,因为用户不能在单个命令中操作所有选定的项目,而是必须一次迭代处理每个项目的列表,但实现起来成本更低.即使在最好的情况下,Cocoa Scripting 也有些蹩脚和不灵活,并且在一次移动/复制/删除多个对象方面非常无望,这是基于 Cocoa 的应用程序中最常见的方法.
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.
使用此属性更新您的 sdef
文件:
Update your sdef
file with this property:
<property name="selection" code="ABCD">
<cocoa key="selectedMyObjects" />
<type type="MyObject" list="yes" />
</property>
有关第一种(更好的)设计方法的示例,请查看脚本编辑器自己的字典.有关第二个的示例,请参阅 Mail 的字典.
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.
(提示:要在脚本编辑器中查看字典,请选择文件>打开字典并从列表中选择适当的项目.要将字典导出为 SDEF 文件,只需确保字典查看器窗口位于最前面并选择文件>另存为.)
(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.)
这篇关于如何指定同一个类的多个 AppleScript 子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!