代码之家  ›  专栏  ›  技术社区  ›  Max Snijders

具有计数但无法访问项的集合

  •  1
  • Max Snijders  · 技术社区  · 7 年前

    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 确实有计数,但我无法从中提取任何项目。我的第一直觉是 塞尔的形状 必须是其他数据类型,但 class sel's shapes 还抛出一个错误,抱怨 塞尔的形状

    问:这里发生了什么?怎么可能 count of 定义(和工作!),同时 item 1 of class of 不是吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Max Snijders    7 年前

    事实证明,这是由于在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
    

    经验教训:注意使用您试图从收藏中获取的正确类别的物品。