I would like to convert the JXA "numMatches" variable in this macro to a KM variable so I can use it for in a "for each, until > numMatches, but don't know how to get the value from JXA to KM. I looked at How to Get and Set KM Variables from JXA? but am having a hard time figuring out if it applies to this situation or how to use it correctly.
The desired result in this case would be KM variable with the value of 5, which is the count of items in the CSV string.
var KME=Application("Keyboard Maestro Engine")
KME.variables["i"].value="5"
KME.variables.push(KME.Variable({'name':"j"}))
KME.variables["j"].value="10"
Here, i was a variable that already existed in Keyboard Maestro, so I could set its value straight away. j did not exist before running this script, so I created it using the push() method, then set its value.
Just to show that the values were received by KM successfully, I got it to display a notification:
You'll have to trust me that those values weren't their original values.
Looks like it works well...I'm not a programmer, so I am not sure how it works...so would I create a new action to run that script or insert it in the current JXA script at the end? I worked at an alternative solution that seems to work to count the items in AppleScript that works, but yours seems more elegant - just need to know where to insert it.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Keyboard Maestro Engine"
set myList to value of variable "csvLine"
set AppleScript's text item delimiters to ","
set numItems to count of text items of myList
end tell
Thanks CJK! I really appreciate your help. I have one more question. The JXA Script creates the variables:
TagName1: apple
TagName2: orange
TagName3: peach
TagName4: pinapple
TagName5: eggs
And I want to be able to paste each of those values "apple,orange,peach,pinapple,eggs" separately into a field. So I did a loop with a end point of 5, and tried to enter the incremental values with a variable TagName%Variable%i% where "i" is the increment. However, the result is that the action pastes the variable NAME "TagName1" instead of the VALUE "apple". If I use the variable "%Variable%TagName1%, then I get the value. Is there a way to increment a variable by 1 for each repeat?
Just so I am clear, the result I am currently getting is:
TagName1
TagName2
TagName3
TagName4
TagName5
and the result I want is:
apple
orange
peach
pinapple
eggs
NOTE that you need to have a Text Edit window open to see the results. THANK YOU.
You're very close with your current macro. All you need to do to ensure the value of each incremented variable is pasted is to add a "Filter with Value of Named Variable" action in between your "Set Variable" actions and your "Insert Text by Pasting" actions:
This filter action replaces the name of a variable (i.e. what you're setting the TagNameNum variable to) with the value of that variable, effectively changing the contents of the TagNameNum variable to "apple" "orange" etc for each loop. In other words, once TagNameNum is set to
TagName1
filtering it with "Value of Named Variable" changes it to
apple
In other words, the value of the TagName1 variable. Make sense? Anyway, try this out and see if it doesn't resolve your issue.
I was trying to figure out the syntax for that, but I kept using an uppercase V. Of course, if I had just looked at the AppleScript dictionary, it would have helped.