@ccstone, @JMichaelTX, @ComplexPoint, @peternlewis or whoever.
NOTE: All the code is stripped-down including removing error checking, to not clutter up the question.
PURPOSE:
I want to copy a trigger from one macro to another. This AppleScript works:
tell application "Keyboard Maestro"
set sourceUUID to "A9904899-5B76-4E29-996C-E6D180A54C79"
set destUUID to "82B0D2C9-C220-4863-BCB3-E15D92DD82A0"
set sourceMacro to first macro whose id is sourceUUID
set sourceTriggerXML to xml of trigger 1 of sourceMacro
set destMacro to first macro whose id is destUUID
tell destMacro
make new trigger with properties {xml:sourceTriggerXML}
end tell
end tell
I have everything working in JXA, except for the make new trigger line at the end:
(function() {
'use strict';
ObjC.import('AppKit');
let km = Application("Keyboard Maestro");
function execute() {
const sourceUUID = "A9904899-5B76-4E29-996C-E6D180A54C79";
const destUUID = "82B0D2C9-C220-4863-BCB3-E15D92DD82A0";
// AS: set sourceMacro to first macro whose id is sourceUUID
let sourceMacro = km.macros.whose({id: {"=": sourceUUID}})[0];
console.log("sourceMacro: " + sourceMacro.name());
// AS: set sourceTriggerXML to xml of trigger 1 of sourceMacro
let sourceTriggerXML = sourceMacro.triggers[0].xml();
console.log("sourceTriggerXML: " + sourceTriggerXML);
// AS: set macro2 to first macro whose id is destUUID
let destMacro = km.macros.whose({id: {"=": destUUID}})[0];
console.log("destMacro: " + destMacro.name());
// HERE'S WHERE I'M STUCK.
// AS:
// tell destMacro
// make new trigger with properties {xml:sourceTriggerXML}
// end tell
destMacro.trigger.make({xml: sourceTriggerXML});
console.log("Did it work?");
return "OK";
}
try {
return execute();
} catch (e) {
return "Error on line: " + e.line + ": " + e.message;
}
})();
I've tried lots of different syntax, but this one looks the closest, because here's the error message:
destMacro.trigger.make({xml: sourceTriggerXML});
Error on line: 28: Parameter is missing.
TO TEST THE CODE:
You need two macros. One with a Trigger, and one without.
Replace the sourceUUID and destUUID in the example code, and you can run it.
HELP!
I know I can modify the triggers collection in JXA, because I have working code that deletes all the triggers. So I know it's not read-only, or something like that.
I just can't get the syntax for adding the trigger!
Help, please!!!???!!!