首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >引用AppleScript中的属性

引用AppleScript中的属性
EN

Stack Overflow用户
提问于 2021-11-09 01:06:00
回答 2查看 196关注 0票数 0

在AppleScript中可以引用属性吗?

我正在为QLab (用于macOS的声音、视频和照明控制)编写一些代码,我希望能够在代码的第一部分中定义/选择/引用某个属性,然后在后面回忆。我希望引用一种属性,然后能够引用另一种属性,而不必重复代码并更改该部分。

QLab的AppleScript字典可以在这里找到

因此,我的例子中没有引用的东西是这样的:

代码语言:javascript
复制
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名称的各种版本

代码语言:javascript
复制
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语句获取对话框中选择的值:

代码语言:javascript
复制
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语句。

我最喜欢这样的东西:

代码语言:javascript
复制
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

有办法这样做吗?

EN

回答 2

Stack Overflow用户

发布于 2021-11-09 22:20:06

我认为您想要做的事情的主要问题是,在应用程序tell语句之外,任何给定应用程序的脚本术语都没有特殊意义。在运行时使用任意脚本术语的一种方法是使用run script命令,因为它可以使用传递给它的文本作为单独的脚本运行。

请注意,这通常被认为是一个糟糕的想法(™),特别是因为尝试使用公共处理程序并向其传递术语通常比使用单独的命令结束的时间更长。您通常还可以获得所有项属性的记录,这也比使用单独的命令更容易。但是,如果您认为您需要这样做,您应该始终验证您计划使用的条款,而不允许用户输入术语(或者如果您也必须这样做,请确保您对它们进行消毒)。

我在文章中没有使用QLab应用程序,因此下面的示例使用系统事件和几种不同的方法来获取文件项的属性。一种方法使用使用预定义术语创建脚本的处理程序,另一种方法从项属性的记录中获取值:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2021-11-10 11:30:16

为了完整起见,only1,下面是如何使用字符串作为键在AppleScript中进行属性查找:

代码语言:javascript
复制
to makePropertyAccessor(appName, propertyName)
    (*
       WARNING: this handler does not sanitize its inputs. 
       NEVER call from untrusted code. Severe risk of data 
       destruction from accidental errors/injection attacks.
    *)
    run script "script
        to getProperty(inObject)
            tell application \"" & appName & "\"
                return " & propertyName & " of inObject
            end tell
        end getProperty
        
        to setProperty(inObject, newValue)
            tell application \"" & appName & "\"
                set " & propertyName & " of inObject to newValue
            end tell
        end setProperty
    end script"
end makePropertyAccessor


-- example usage:

set ContainerAccessor to makePropertyAccessor("Finder", "container")

tell application "Finder" to set aRecord to properties of home

ContainerAccessor's getProperty(aRecord)
--> folder "Users" of startup disk of application "Finder"

与大多数代码生成一样,这是完全邪恶和不安全的,而且如果您使用搞砸,则很有可能造成严重的破坏。虽然我意识到输入一个冗长的if块有些乏味,但是这样的代码非常简单、清晰、可靠,而且尽可能地证明它是愚蠢的。而且AppleScript已经够简陋的了,上面没有任何“聪明的黑客”。吻一下。

--

1我根本不会发布它,只是@red_menace的实现是有问题的。1.它完全不安全,但它没有提到;2.它跳过不同的解释器上下文来完成这项工作,因此速度慢,不能在不破坏引用和其他复杂值的情况下传递引用和其他复杂值。这并不意味着我的任务没有问题:除了完全不安全之外,我还隐约记得run script会在每个调用中泄漏内存。但是,它只需要对您想要访问的每个属性调用一次,并且返回的可重用对象在当前上下文中执行,因此在其处理程序中传递值没有问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69891671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档