How Can I Access Dictionary From JXA?

Manual has example for AppleScript.
You can read and write dictionary values from AppleScript.

tell application "Keyboard Maestro Engine"
	name of dictionaries
end tell

tell application "Keyboard Maestro Engine"
	dictionary keys of dictionary "First Names"
end tell

tell application "Keyboard Maestro Engine"
	set value of dictionary key "P" of dictionary "First Names" to "Fred"
end tell

Could someone translate to JXA?
Also, how can I remove a key from a dictionary, and delete a dictionary?
Thanks!

I figured out the reading/writing/delete. The testing script is below in case others are interested.
If I just delete all the keys in a dictionary, the dictionary gets deleted. Is there an easier way to delete a dictionary?
Also, I still couldn’t figure out how to create a dictionary or add a key to an existing dictionary using JXA.
I’d appreciate if someone could let me know.
Thanks!

// Loop through the dictionaries and their keys.
    kme = Application("Keyboard Maestro Engine")
    dictionaries = kme.dictionaries
    console.log("There are "+dictionaries.length+" dictionaries.")
    for (d in dictionaries) {
    	dictionary = dictionaries[d]
    	keys = dictionary.dictionaryKeys
    	console.log(dictionary.name()+" has "+keys.length+" keys.")
    	for (k in keys) {
    		key = keys[k]
    		console.log(key.name()+": "+key.value())
    	}
    }
// Set a value for a key
    dictionaries["testDictionary"].dictionaryKeys["key1"].value = "edited value 1"
    console.log(dictionaries["testDictionary"].dictionaryKeys["key1"].value())
// Delete a key
    dictionaries["testDictionary"].dictionaryKeys["key1"].value = ""

Not, that's how a dictionary works. It exists if it has any contents.

I am no expert in JXA, but doesn't this:

    dictionaries["testDictionary"].dictionaryKeys["key1"].value = "edited value 1"

Create dictionaries/keys?

Hi @peternlewis, thanks for the response.
dictionaries[“testDictionary”].dictionaryKeys[“key1”].value =
"edited value 1"
just edits the value of key1.
If key1 doesn’t exist, the same statement just throws "can’t get object"
error.
How do you create a dictionary and a dictionary key in AppleScript?
Thanks,

Well done! Thanks for sharing.

I'm having the same issue. Here's a script that creates a new KM Variable successfully, but FAILS when trying to use the same pattern with creating a new KM Dictionary:

var appKM = Application("Keyboard Maestro Engine")

var kmVarName = "TEST__JXA";

//---- WORKS for Creating a New KM Variable ----

var oKMVar =  appKM.variables.push(
                appKM.Variable({
                  'name': kmVarName,
                  'value': "Created from JXA -- TEST 3"
                })
              );
      
var kmDictName = "TEST__JXADict";
var kmDictKey  = "JXA1";
        
// ### ERRORS When Trying to Create a New KM Dictionary ###
var oKMDict = appKM.dictionaries.push(
      appKM.Dictionary({
        'name':    kmDictName,
        'key':    kmDictKey,
        'value':  "Created from JXA -- TEST 1"
        })
      );
      
//-->Error on line 18: Error: Can't convert types.

Maybe you or someone can use this to help figure out the solution.

Maybe @ComplexPoint, our resident JXA guru, can give us some guidance here.

Thanks for the reply.
Based on what you provided, I tried the following, but the last statement throws "AppleEvent handler failed. (-10000)"
Maybe getting closer? :slight_smile:

dictionary = dictionaries[“dictionary1”]
key = kme.DictionaryKey({name:“key1”, value:“value1”})
dictionary.dictionaryKeys.push(key)

You probably know this, but the following also works for variables. If
the variable exists, it changes its value. If not, it just creates a new
one.
kme.setvaiable(“variableName” {to:“variableValue”})

Good try.

I believe that with JXA to create a new object in a collection, you need to use either the .push() method or the .make() method.

I didn't show it my above code, but I also tried the .make() method (which created the object without any data/properties), followed by statements to set the properties. But it failed as well.

Yep, that is what I use mostly. It became available with KM 7.1, I believe.
But there is no analog for KM Dictionaries.

@peternlewis, what do you think about adding a scripting command for Dictionaries like we have have Variables?

@chi, looks like it is NOT possible to create a KM Dictionary directly via script (either JXA or AppleScript).

See: