Dynamically name apple script variable

1.2k Views Asked by At

Let me preface this by saying that I know that I can easily do the following using a calculated Apple Script from Filemaker. I'd really love to know how to accomplish this inside an Apple Script. I'm trying to set variables into a macro program called Keyboard Maestro that I will refer to as KM.

My script first steps through found records in Filemaker and copies data from each found record. I can't figure out how to set a KM Variable from a loop in Apple Script because, as far as I know, you can't dynamically set the names of Apple Script variables.

I can create the names (that i would like to use) of the variable by creating a list with:

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

--

But when I set the contents of my fields to the list items that I created, the variables don't show up in 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

How can I setup these KM variables?

1

There are 1 best solutions below

2
Pat_Morita On

You cannot create dynamic variables with applescript.

What you currently do is to generate strings and add them to a list. Then you REPLACE these strings with other values so the strings are lost.

But you could achieve this with the help of 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

Then you can access the dynamic values with

set myValue to funderNameDict's objectForKey:"yourDynamicVarName"

to loop through the dictionary use

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