Dynamically set KM Variables using Action Script

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 step through found records in Filemaker and copy data from each found record into a KM Variable. 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?

Actually, it is quite easy. Yes, you CAN dynamically create and set KM Variables from AppleScript. Here's how:

set someVarStr to "data from Filemaker"

tell application "Keyboard Maestro Engine"
   -- IF Variable does NOT exist, it will be created --
   setvariable "My KM Var Name" to someVarStr
end tell

For more info, see AppleScript (KM Wiki).

1 Like

Got it and figured out what I was doing wrong. I was trying to set the name from a value in the list. I am now setting it by concatenating with strings and the i variable I created. Thanks so much for the help!!

Here’s the final code that is working now.

tell application "FileMaker Pro Advanced"
	
	tell document "Database"
		go to layout "Merchants_ExistingAdvances_AS"
		show (every record whose cell ¬
			"fk_Merchants" contains "2092") -- find 
		set winCount to count records of front window
		
		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
		
		
		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"
					
					set funderName to "Business_ExistingAdvance" & i & "FunderName"
					set FundingCurrentBalance to "Business_ExistingAdvance" & i & "FundingCurrentBalance"
					set FundingAmount to "Business_ExistingAdvance" & i & "FundingAmount"
					setvariable funderName as text to item i of variableListFunderName
					setvariable FundingCurrentBalance as text to item i of variableListFundingCurrentBalance
					setvariable FundingAmount as text to item i of variableListFundingAmount
				end tell
			end ignoring
			set i to i + 1
		end repeat
		
		
	end tell
end tell
1 Like