1

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?

Jamey16
  • 21
  • 9

1 Answers1

1

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 
Pat_Morita
  • 3,355
  • 3
  • 25
  • 36
  • 1
    Thank you for your help with this. It was the KM variable that I needed to set and as it turns out I can set them dynamically. It wasn't working because I had to declare them "as text" in the apple script. I will very likely run into circumstances where I will need to use your method outlines above so this will untimely be helpful to me. Thanks again! – Jamey16 Apr 29 '17 at 00:04
  • I voted your response btw, however it may not reflect due to my Newbie status. – Jamey16 Apr 29 '17 at 00:05