How to export *selected* graphics using JXA or OmniJS?

I have an AppleScript that exports each top-level graphic from an OmniGraffle canvas to one PDF file per graphic. It works fine, but I would like to re-implement it in JXA or (even better) OmniJS; unfortunately with each JavaScript-based option I have hit a barrier.

The AppleScript works by looping thru all the graphics on the active canvas, selecting each in turn, exporting selected graphics, and then moving on to the next. It works fine.

But with JXA, I cannot work out how to select each graphic. Assigning to the window.selection property just seems to clear the selection, not select the graphic. Other than that critical aspect, the JXA works.

And with OmniJS, I cannot work out how to export selected graphics. There seems to be no document.export() method which JXA has, and FileWrappers don’t seem to offer a way to write to disk, nor to export only selected graphics. Not that I can work out anyway.

I realise a somewhat similar question was asked three years ago (OmniJS: Export image files from selection) but I’m hoping that there is either a JXA-based solution, or (ideally) that export has become available in OmniJS in the meantime.

Does anyone have any tips? Any assistance would be greatly appreciated :)

(PS: The reason I want to do this in OmniJS is that this script is part of a larger suite of scripts that I use with OmniGraffle, most of which are OmniJS plug-ins, and so I would prefer to have all the scripts be OmniJS, rather than 80% OmniJS and 20% AppleScript simply because I cannot work out how to implement something in OmniJS.)

(PPS: The reason I want to export each top-level graphic to its own PDF is that my real script exports each graphic to PDF along with its coordinates on the canvas. I then have a second script that imports each PDF into PowerPoint, and positions each graphic using its coordinates. This effectively imports the OmniGraffle diagram into PowerPoint, but in a way that allows me to animate the individual parts of the diagram, and retains much better visual fidelity than exporting and importing via SVG.)

For reference, here is a simplified version of the AppleScript, which works:

#!/usr/bin/env osascript

on run
    tell application "OmniGraffle"
        set activeWindow    to the front window
        set activeDocument  to the document     of the activeWindow
        set activeCanvas    to the canvas       of the activeWindow
        set allGraphics     to every graphic    of the activeCanvas

        set folderAlias to choose folder

        repeat with theGraphic in allGraphics
            set graphicName to the user name of theGraphic
            set filePath    to folderAlias as text & graphicName & ".pdf"
            log filePath

            set the selection of the activeWindow to { theGraphic }
            export the activeDocument as "PDF" scope selected graphics to file filePath with properties { drawsbackground: false }
        end repeat
    end tell
end run

and here is the JXA, with the problematic line marked with ***:

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

"use strict";

function run ()
{
    const app = Application("OmniGraffle");
    app.includeStandardAdditions = true;

    const activeWindow    = app.windows[0];
    const activeDocument  = activeWindow.document();
    const activeCanvas    = activeWindow.canvas();
    const allGraphics     = activeCanvas.graphics();

    const folderPath = app.chooseFolder();

    for (const theGraphic of allGraphics) {
        const graphicName = theGraphic.userName();
        const filePath    = folderPath.toString() + "/" + graphicName + ".pdf"
        console.log(filePath);

        // *** THE NEXT LINE TRIES TO SELECT theGraphic ...
        activeWindow.selection = [ theGraphic ];
        // *** ... BUT THE RESULT IS THAT ALL GRAPHICS ARE DE-SELECTED

        activeDocument.export({
            as:             "PDF",
            scope:          "selected graphics",
            to:             filePath,
            withProperties: { drawsbackground: false }
        });
    }
}

and here is the OmniJS, with the problematic code commented out and marked with ***:

/*{
    "type": "action"
}*/

(() => {
    "use strict";

    function DoAction ()
    {
        const activeDocument  = document;
        const activeWindow    = activeDocument.windows[0];
        const activeCanvas    = activeWindow.selection.canvas;
        const allGraphics     = activeCanvas.graphics;

        var picker = new FilePicker()
        picker.folders  = true
        picker.multiple = false

        picker.show().then(
            urls => {
                const folderURL = urls[0].toString();

                for (const theGraphic of allGraphics) {
                    const graphicName = theGraphic.name;
                    const fileURL     = folderURL + graphicName + ".pdf"
                    console.log(fileURL);

                    activeWindow.selection.view.select([ theGraphic ]);

                    /* *** WHAT TO WRITE INSTEAD OF THIS? ***
                    activeDocument.export({
                        as:             "PDF",
                        scope:          "selected graphics",
                        to:             fileURL,
                        withProperties: { drawsbackground: false }
                    });
                    */
                }
            }
        );
    }

    return new PlugIn.Action(DoAction);
})();

I’ve simply modify a little bit your program to make it handle no user named shapes:

#!/usr/bin/env osascript

on run
    tell application "OmniGraffle"
        activate
        set activeWindow    to the front window
        set activeDocument  to the document     of the activeWindow
        set activeCanvas    to the canvas       of the activeWindow
        set allGraphics     to every graphic    of the activeCanvas

        set folderAlias to choose folder

        set i to 1
        repeat with theGraphic in allGraphics
            if  (exists the user name of theGraphic) then
                set graphicName to the user name of theGraphic
            else
                set graphicName to "gn" & i
                set i to i + 1
            end if
            set filePath    to folderAlias as text & graphicName & ".pdf"
            log filePath

            set the selection of the activeWindow to { theGraphic }
            export the activeDocument as "PDF" scope selected graphics to file filePath with properties { drawsbackground: false }
        end repeat
    end tell
end run