首页 > 解决方案 > 集合有计数但无法访问项目

问题描述

我正在尝试通过 AppleScript 自动化一些 MS PowerPoint 的东西。我想获取活动窗口的(形状)选择。读字典,我的猜测是:

tell application "Microsoft PowerPoint"
    set sel to shape range of selection of active window

    count of sel's shapes -- returns 2 for specific case
    -- class of sel's shapes -- throws a compilation error "object you are trying to access does not exist"
    set i to item 1 of sel's shapes -- i not set but this line does not throw an error
    i -- error: the variable i is not defined
end tell

注释指示运行特定行时会发生什么。有趣的是,sel's shapes确实有计数,但我无法从中获取任何项目。我的第一直觉是它sel's shapes必须是其他数据类型,但class sel's shapes也会引发错误,抱怨sel's shapes不存在。

问:这是怎么回事?如何count of定义(和工作!)whileitem 1 ofclass ofnot?

标签: applescript

解决方案


事实证明,这是由于 AppleScript 中处理参考表单的方式(https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html#//apple_ref/doc /uid/TP40000983-CH4g-120522)。

在这种情况下,“形状范围”不包含任何“项目”。它只包含“形状”:

set s to shape 1 of shape range of selection of active window

工作得很好:

s's left position -- Returns an actual value

经验教训:注意使用您尝试从集合中获取的正确项目类别。


推荐阅读