动态命名apple脚本变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态命名apple脚本变量相关的知识,希望对你有一定的参考价值。

让我先说一下,我知道我可以使用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变量?

答案

您无法使用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 

以上是关于动态命名apple脚本变量的主要内容,如果未能解决你的问题,请参考以下文章

常用python日期日志获取内容循环的代码片段

Python pandas:动态命名加载的 CSV 文件

ServiceNow代码规范

Python-根据另一个变量重命名重复值

Python动态执行代码修改命名空间

尝试创建动态合并脚本并动态传递变量