我是否可以仅使用对象说明符获取集合中每个元素的特定属性?

时间:2012-03-29 21:37:50

标签: applescript

说我有一个收藏品。我是否可以仅使用object specifiers(例如,没有looping statement)获取集合元素的特定属性的值列表?如果是这样,怎么样?例如,记录一系列记录:

set stuff to {{foo:"bar"}, {foo:"baz"}, {foo:"bam"}}

对于此示例,结果应该是foo属性的值列表:

{"bar", "baz", "bam"}

为了说明“仅对象说明符”的含义,生成此列表的语句应如下所示:

foo of each item of stuff

除了没有each参考表格之外。使用repeat的答案没有得分。如果包含足够的证据,那么“不,你不能那样做”的答案是完全可以接受的。

1 个答案:

答案 0 :(得分:4)

filter reference form仅指定应用程序对象,不能在常规AppleScript对象(如列表或记录)上使用,因此您必须使用应用程序对其中一个对象进行过滤,例如< / p>

tell application "System Events" to return value of every property list item of property list items of (make new property list item with properties {value:stuff}) whose name is "foo"

另一种方法是使用 AppleScriptObjC (通过像Cocoa-AppleScript applet或ASObjC Runner之类的东西),你可以使用NSArray的 valueForKey:方法,例如

set stuff to current application's NSArray's arrayWithArray_({{foo:"bar"}, {foo:"baz"}, {foo:"bam"}})
stuff's valueForKey_("foo") as list