【问题标题】:Dynamically name apple script variable动态命名苹果脚本变量
【发布时间】:2017-09-25 23:27:41
【问题描述】:

让我先说一下,我知道我可以使用 Filemaker 计算的 Apple 脚本轻松地执行以下操作。我真的很想知道如何在 Apple Script 中实现这一点。我正在尝试将变量设置到一个名为 Keyboard Maestro 的宏程序中,我将其称为 KM。

我的脚本首先遍历 Filemaker 中找到的记录,并从每个找到的记录中复制数据。我不知道如何在 Apple Script 的循环中设置 KM 变量,因为据我所知,您无法动态设置 Apple Script 变量的名称。

我可以通过创建一个列表来创建变量的名称(我想使用):

set variableListFunderName to {}
set i to 1
repeat until i = winCount + 1

    copy "Business_ExistingAdvance" & i & "FunderName" to the end of variableListFunderName
    set i to i + 1
end repeat



set variableListFundingCurrentBalance to {}
set i to 1
repeat until i = winCount + 1

    copy "Business_ExistingAdvance" & i & "FundingCurrentBalance" to the end   of variableListFundingCurrentBalance
    set i to i + 1
end repeat




set variableListFundingAmount to {}
set i to 1
repeat until i = winCount + 1

    copy "Business_ExistingAdvance" & i & "FundingAmount" to the end of variableListFundingAmount
    set i to i + 1
end repeat

--

但是当我将字段的内容设置为我创建的列表项时,变量不会显示在 KM 中:

--

set i to 1


repeat until i = winCount + 1
    tell record i

        set item i of variableListFunderName to cell "Funders_ExistingAdvances::FunderCompanyName"
        set item i of variableListFundingCurrentBalance to cell "FundingCurrentBalance"
        set item i of variableListFundingAmount to cell "FundingAmount"
    end tell

    ignoring application responses
        tell application "Keyboard Maestro Engine"

            setvariable item i of variableListFunderName & "_AS" to item i of variableListFunderName
            setvariable item i of variableListFundingCurrentBalance & "_AS" to item i of variableListFundingCurrentBalance
            setvariable item i of variableListFundingAmount & "_AS" to (item i of variableListFundingAmount)
        end tell
    end ignoring
    set i to i + 1
end repeat

如何设置这些 KM 变量?

【问题讨论】:

    标签: variables dynamic set applescript keyboard-maestro


    【解决方案1】:

    您不能使用 applescript 创建动态变量。

    您当前所做的是生成字符串并将它们添加到列表中。然后你用其他值替换这些字符串,这样字符串就会丢失。

    但是你可以在 applescript Objective-c 的帮助下实现这一点:

    use framework foundation
    
    set variableListFunderName to {}
    set i to 1
    repeat until i = winCount + 1
    
        copy "Business_ExistingAdvance" & i & "FunderName" to the end of variableListFunderName
        set i to i + 1
    end repeat
    
    set funderNameDict to current application's NSMutableDictionary's alloc()'s init()
    
    repeat with var in variableListFunderName
     funderNameDict's setObject:"yourValue" forKey:var
    end repeat
    

    然后你可以访问动态值

    set myValue to funderNameDict's objectForKey:"yourDynamicVarName"
    

    遍历字典使用

    repeat with theKey in funderNameDict's allKeys()
    set myValue to funderNameDict's objectForKey:theKey
    end repeat 
    

    【讨论】:

    • 感谢您对此的帮助。这是我需要设置的 KM 变量,事实证明我可以动态设置它们。它不起作用,因为我必须在苹果脚本中将它们声明为“文本”。我很可能会遇到需要使用上述方法概述的情况,因此这对我很有帮助。再次感谢!
    • 顺便说一句,我对您的回复投了赞成票,但由于我的新手身份,它可能无法反映。
    猜你喜欢
    • 2010-10-20
    • 1970-01-01
    • 2011-08-18
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多