Creating Variables or a Dictionary From Tokens

Sometimes it would be handy to take the tokens that contain a lot of values and break them up into variables or maybe create a dictionary. You can do this one action at a time with Set Variable to Text and using the token components there.

Or you could build a JSON from the tokens and then use the Set Variables to JSON or Set Dictionary to JSON to break up the values. My example is similar to the example at the bottom of token:Window Tokens [Keyboard Maestro Wiki]. That example uses the Calculate token to extract the different sub-values.

In my example the blue is where we set a variable to the JSON values we're interested in. The format of the JSON is:
{
"key1" : "value1",
"key2" : "value2"
}

Braces around the whole thing. Key names in double-quotes, followed by a colon (spaces around the colon optional), value in double-quotes and if there are more key/values a comma.

The green section shows how to create an Instance variable for each key/value pair (so they can be used in macros that call this one). The key name is appended to the prefix value in the dialog and the variable assigned the value. My example creates 9 variables. The Display text shows the values for 4 of them.

The orange section does the same thing as the green except breaks the values up into Dictionary key name/value pairs.

Macro:
Window Coordinates to JSON to Variable or Dictionary.kmmacros (7.1 KB)

2 Likes

Hi @kvanh - This is brilliant. Thanks for sharing. I've only just learnt (today, through the power of Google search) to pronounce JSON as "Jason". I suppose it is like .jpeg "Jaypeg" :grinning: