AppleScript类型强制转换异常:仅特定场景触发的问题咨询
问题解析:两种AppleScript写法的差异原因
这两种写法的核心差异在于System Events返回的position属性的类型处理时机:
成功的写法:
当你把position of ...赋值给xyPositions时,AppleScript会自动将System Events返回的专有posn类型(包含x、y坐标的结构化对象)转换为AppleScript原生的列表类型({x值, y值})。后续的item 1 of xyPositions是在操作原生列表,自然能正确获取x坐标。失败的写法:
直接写item 1 of position of ...时,整个表达式会被作为一个AppleEvent发送给System Events处理。但System Events的AppleEvent接口不支持对position属性直接执行下标访问——它只能返回完整的posn对象,无法解析item 1这个指令,因此会触发类型转换错误。
解决方案
如果不想用中间变量,可以通过强制类型转换让AppleScript先把posn转为列表,再取元素:
activate application "NotificationCenter" tell application "System Events" tell process "Notification Center" set xPos to item 1 of (position of group 1 of UI element 1 of scroll area 1 of window "Notification Center" as list) end tell end tell
内容的提问来源于stack exchange,提问作者seren
相关产品推荐
相关产品推荐

