applescript - 引用 AppleScript 中的属性
问题描述
是否可以在 AppleScript 中引用属性?
我正在为 QLab(macOS 的声音、视频和照明控制)编写一些代码,我希望能够在代码的第一部分定义/选择/引用某个属性,然后再调用。我希望引用一种属性,然后能够引用其他属性,而无需复制我的代码并更改该部分。
所以我没有参考就可以工作的例子是这样的:
tell application id "com.figure53.QLab.4" to tell front workspace
set selectedCues to last item of (selected as list) -- selects an item in my software (QLab)
return q name of selectedCues -- This works and returns q name of selectedCues
end tell
我也想做同样的事情,但引用属性“q name”并稍后调用它
在我的脚本中,我尝试了将变量(选定属性)设置为 q 名称的各种版本
tell application id "com.figure53.QLab.4" to tell front workspace
set selectedCues to last item of (selected as list) -- selects an item in my software (QLab)
set selectedProperty to q name -- This code doesn't work
return selectedProperty of selectedCues -- This is broken
end tell
编辑:添加了新示例以更好地解释我在寻找什么:
我在这里编写了一个脚本,它带有一个处理程序,该处理程序返回 selectedCues 的选定属性(我的软件中的项目)。它允许用户从列表中选择,然后使用 if 语句获取对话框中选择的值:
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 {}
if cueProperty is "q name" then -- Using if statements here to make returnedValue return selected property
repeat with i in cuesToProcess
set end of returnedValue to (q name of i)
end repeat
else if cueProperty is "q number" then
repeat with i in cuesToProcess
set end of returnedValue to (q number of i)
end repeat
else if cueProperty is "q type" then
repeat with i in cuesToProcess
set end of returnedValue to (q type of i)
end repeat
else if cueProperty is "q color" then
repeat with i in cuesToProcess
set end of returnedValue to (q color of i)
end repeat
else if cueProperty is "notes" then
repeat with i in cuesToProcess
set end of returnedValue to (notes of i)
end repeat
end if
returnedValue
end tell
end returnPropertyOfSelected
这样做我必须为用户可以做出的每一个选择做一个 if 语句。我想知道是否有办法让这个选择被引用并直接实现到代码中,这样我就不必为每个可能的选择做一个 if 语句。
理想情况下,我想要这样的东西:
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 end of returnedValue to (cueProperty of i) -- This is the part I would like, but it doesn't work
end repeat
returnedValue
end tell
end returnPropertyOfSelected
有没有办法做到这一点?
解决方案
我认为您正在尝试做的主要问题是,在应用程序告诉语句之外,任何给定应用程序的脚本术语都没有特殊含义。在运行时使用任意脚本术语的一种方法是使用run script
命令,因为它可以使用传递给它的文本作为单独的脚本来运行。
请注意,这通常被认为是一个 Bad Idea™,尤其是因为尝试使用通用处理程序并将术语传递给它通常会比仅使用单独的命令更长。您通常还可以获得所有项目属性的记录,这也比使用单独的命令更容易。但是,如果您认为您需要做这样的事情,您应该始终验证您计划使用的条款,并且不允许用户输入条款(或者如果您也必须这样做,请确保对它们进行清理)。
我没有在您的帖子中使用QLab应用程序,因此以下示例使用系统事件和几种不同的方法来获取文件项的属性。一种方法是使用使用预定义术语创建脚本的处理程序,另一种方法是从项目属性的记录中获取值:
property itemClass : "class"
property itemType : "type identifier" -- UTI
property itemName : "name"
property itemDefault : "default application" -- alias
set fileItem to (choose file)
log runScript(itemType, fileItem) -- get property by creating a script using a term
tell application "System Events" to set itemProperties to (get properties of disk item (fileItem as text))
log type identifier of itemProperties -- get property from a record of all the properties
to runScript(fileProperty, fileItem) -- get specified file property
return run script "tell application \"System Events\" to return " & fileProperty & " of disk item " & space & quote & fileItem & quote
end runScript
推荐阅读
- twitter - Ionic 3 的 Twitter Connect 插件中的登录会话失败
- python - 通过 pandas 绘图界面使用颜色条分组散点图
- sql-server - 如何在同一张桌子上获得不同结果的计数
- php - 将动态数组与动态键组合成单个数组
- aws-glue - AWS glueContext 读取不允许 sql 查询
- maven - 未找到 BIRT 运行时
- java - 如何从 solace 中获取所有队列和主题
- c++ - OpenGL - 纹理显示不正确(夹紧?)
- android - Youtube 播放器与 Android 应用程序中的缩略图集成
- reactjs - React - 如何处理大量动态输入的更改?