Continuing the discussion from Creating KM dictionaries from Scripts:
@peternlewis, based on the script by @ComplexPoint, and my own testing (both AppleScript and JXA), it does not appear possible to create KM Dictionaries using direct scripting terminology in the KM Engine. @ComplexPoint had to resort to using dynamically created KM Macros, which were then executed using the doScript
command.
If I’m missing something, and there is a more direct way in either AppleScript or JXA, please advise.
Here is my test script, based on the JXA functions provide by @ComplexPoint.
This works, but does require use of dynamically created KM Macro.
JXA Test Script to Create a Dictionary
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Creating @KM @Dictionaries from Script @JXA
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DATE: 2018-02-15
REF:
• Creating KM dictionaries from Scripts by @ComplexPoint
• https://forum.keyboardmaestro.com/t/creating-km-dictionaries-from-scripts/8249
*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Function to Create KM Dictionary with Key and Value
// • Apparently this cannot be done with normal AppleScript or JXA
// • Appears to Require Use of Dynamically Created KM Macro
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// kmDictValueSet :: String -> String -> String -> KM ()
const kmDictValueSet = (strDict, strKey, strValue) =>
Application('Keyboard Maestro Engine')
.doScript(plistMay([{
MacroActionType: 'SetDictionaryValue',
Dictionary: strDict,
Key: strKey,
Value: strValue
}])
.just);
// plistMay :: JS Object -> Maybe Plist String
const plistMay = jso => {
var error = $();
const maybeString = $.NSString.alloc.initWithDataEncoding(
$.NSPropertyListSerialization
.dataWithPropertyListFormatOptionsError(
$(jso),
$.NSPropertyListXMLFormat_v1_0,
0,
error
),
$.NSUTF8StringEncoding
);
return Boolean(error.code) ? {
nothing: true,
msg: error.localizedDescription
} : {
nothing: false,
just: ObjC.unwrap(maybeString)
};
};
//~~~~~~~~~~~~~~~~~~~~~~ MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var kmDictName = "JXA_Dict1";
var kmKeyName = "JXA_K1";
var kmKeyValue = "Created by JXA";
var oDict = kmDictValueSet(kmDictName, kmKeyName, kmKeyValue)
var appKME = Application("Keyboard Maestro Engine")
var kmDictList = appKME.dictionaries.name();
kmDictList
If I am correct, please consider this as a feature request to provide explicit scripting commands to create KM Dictionaries, and KM Dictionary Keys.