Struggling to Fix This Double Quotation Escaping Problem


I've got a variable action named instance_element_list inside a loop set to continue to append to itself until the loop has finished.

"%Element value%"

NOTE: that instance_element includes quotation marks.

After the loop has finished, that instance_element_list will be contain an array including quotation marks as I have mentioned.
eg. "Fish","Cook"
NOT Fish,Cook

now I am trying to retrieve it via JXA. That jXA code looks like this:

		list_of_fields_w_value = field_name + ":  ["+instance_element_list+"]"

Later , my form action should present it like this

hobbies : ["Fish","Cook"]

Quotation marks are not escaping properly. Instead, I keep getting this:

hobbies: [+instance_element_list+]

Can you put the example in a toy KM macro? If I can reproduce the issue then maybe I could play around with escaping in different ways to try to achieve your desired result

Keyboard Maestro Actions.kmactions (2.2 KB)

Keyboard Maestro Export

For this example, I just used one array element

I just wondered whether you managed to fix the problem im having.

I think I don't understand how variables expand in this language (bash?).

It looks like the ": [+element_list+]" is in a literal string and would not get a value - but perhaps I don't understand this enough

I think @kraftyDevil is right and your provided code is concating the value of field_name with a string literal.

I assume your "form action" should be presented as a text string as well? Try building your string by looping through element_list to get eg '"Cat","Dog"' and then wrapping that up with field_name and the punctuation characters.

I'm more AppleScript than JXA, but I'll see if I can knock up an example.

OK... A quick example to transform an array of strings into a single string in the format you wanted. Turns out you can use "join", rather than loop through the array (said I was a JavaScript newb!):

'use strict';
(function run() {

	var element_list = ['cat','dog','fish'];
	var field_name = 'Hobbies';

	var list_of_fields_w_value = field_name + ': ["' + element_list.join('","') + '"]';



Hopefully the single quotes around the strings will make it more obvious where we're inserting the double quotes! The above assumes that, in your script, "km.getvariable" will set "element_list" to an array of strings. But I've an horrible feeling it will be a delimited (comma, by default) text string eg "cat, dog,fish". In that case you can split and join (change your split delimiter to suit):

	// other stuff...
	var element_list = "cat,dog,fish";
	var element_list = element_list.split(",");
	// more stuff...

Not sure what you are doing with the "list_of_fields_w_value" variable -- you seem to be initiating it with a KBM variable and then overwriting that 2 lines later. But perhaps I'm missing a trick.

Thanks will test this out soon. Just trying to sort out a different problem.