AppleScript如何动态引用QLab的Cue属性以避免重复分支代码?
AppleScript动态引用QLab属性实现方案
AppleScript原生不支持直接将属性名以字符串形式存储后直接调用[属性字符串] of 对象的语法,但可以通过动态构造脚本执行的方式实现无分支的动态属性读取,新增属性仅需在参数列表里添加对应属性名字符串即可,无需修改后续逻辑。
可运行完整代码
tell application id "com.figure53.QLab.4" to tell front workspace set selectedCues to (selected as list) set parameterChoices to {"q name", "q number", "q type", "q color", "notes"} choose from list parameterChoices with prompt "Pick a property to return from selected cue" with title "Choose a property" default items {"q name"} set selectedProperty to item 1 of result end tell returnPropertyOfSelected(selectedProperty, selectedCues) on returnPropertyOfSelected(cueProperty, cuesToProcess) tell application id "com.figure53.QLab.4" to tell front workspace set returnedValue to {} repeat with i in cuesToProcess -- 核心逻辑:动态构造脚本语句执行获取属性 set getPropScript to "get " & cueProperty & " of item " & (index of i as text) & " of (selected as list)" set propValue to run script getPropScript set end of returnedValue to propValue end repeat return returnedValue end tell end returnPropertyOfSelected
原理说明
- 核心依赖AppleScript内置的
run script命令,可以将拼接好的字符串作为脚本代码实时执行,完美绕过静态属性解析的限制 - 新增支持的属性时,只需要在
parameterChoices列表里添加QLab AppleScript字典中对应的属性名即可,不需要新增任何判断分支 - 注意属性名拼写必须和QLab官方提供的AppleScript API定义完全一致,大小写、空格都不能出错,否则会触发执行错误
内容的提问来源于stack exchange,提问作者Baldrian Sector
相关产品推荐
相关产品推荐

