Creating UTM VM's using JXA

Hi all,

I was looking for some assistance with utilizing the AppleScript scripting interface for UTM from JXA. From reading the UTM.sdef from the Script Editor in JavaScript language mode, I came up with the following working example that lists VM’s and give output in JSON format to STDOUT:

#!/usr/bin/env osascript -l JavaScript

const app = Application('UTM');
let vms = app.virtualMachines().map((vm) => {
    return JSON.parse(Automation.getDisplayString(vm.properties()));
});

// Output last statement to stdout
JSON.stringify(vms);

I am interested in creating VM’s as well, but have not had much luck. Here is what I have attemped thus far:

Attempt 1:

#!/usr/bin/env osascript -l JavaScript
const utm = Application("UTM");
let config = utm.QemuConfiguration(
    {
        "name":"test",
        "notes":"",
        "architecture":"aarch64",
        "memory":4096,
        "cpuCores":0,
        "hypervisor":true,
        "uefi":true,
        "directoryShareMode":"VirtFS",
        "drives":[],
        "networkInterfaces":[],
        "serialPorts":[],
        "machine":"virt"
    }
);

var vm = utm.VirtualMachine(
    {
        "backend":"qemu",
        "configuration": config
    }
);
utm.virtualMachines.push(vm);

Attempt 2:

#!/usr/bin/env osascript -l JavaScript
const utm = Application('UTM');
const config = utm.QemuConfiguration(
    {
        "name":"test",
        "notes":"",
        "architecture":"aarch64",
        "memory":4096,
        "cpuCores":0,
        "hypervisor":true,
        "uefi":true,
        "directoryShareMode":"VirtFS",
        "drives":[],
        "networkInterfaces":[],
        "serialPorts":[],
        "machine":"virt"
    }
);
const vm = utm.make(
    {
        "new": "virtualMachine",
        "withProperties":   {
            "backend"   :"qemu",
            "configuration" : config,
        }
    }
);

Attempt 3:

#!/usr/bin/env osascript -l JavaScript
const utm = Application('UTM');

const vm = utm.make(
    {
        "new": "virtualMachine",
        "withProperties":   {
            "backend"   :"qemu",
            "configuration" : {
                "name":"test",
                "notes":"",
                "architecture":"aarch64",
                "memory":4096,
                "cpuCores":0,
                "hypervisor":true,
                "uefi":true,
                "directoryShareMode":"VirtFS",
                "drives":[],
                "networkInterfaces":[],
                "serialPorts":[],
                "machine":"virt"
            }
        }
    }
);

Attempts 1 and 2 yield: execution error: Error: Error: A valid configuration must be specified. (-2700)
Attempt 3 yields: execution error: Error: Error: Can't convert types. (-1700)

I think I am close here, but I could use some assistance.

Here is the equivalent AppleScript:

tell application "UTM"
    --- specify a boot ISO
    set iso to POSIX file "/path/to/ubuntu.iso"
    --- create a new QEMU VM for ARM64 with a single 64GiB drive
    set vm to make new virtual machine with properties {backend:qemu, configuration:{name:"QEMU ARM64", architecture:"aarch64", drives:{{removable:true, source:iso}, {guest size:65536}}}}
    --- note the default options for a new VM is no display, one PTTY serial port, and one shared network
    --- duplicate an existing VM and disable hypervisor in the duplicate
    duplicate vm with properties {configuration:{name:"Duplicate QEMU ARM64", hypervisor:false}}
    --- create an Apple VM for booting Linux (only supported on macOS 13+)
    make new virtual machine with properties {backend:apple, configuration:{name:"Apple Linux", drives:{{removable:true, source:iso}, {guest size:65536}}}}
end tell