Execute omniJS code in OmniGraffle 7.5 test builds (Sierra & onward)

Execute omniJS script in OmniGraffle Test 7.5.zip (11.0 KB)

Custom Keyboard Maestro Plug-in

NAME

  • Execute omniJS script in Omnigraffle Test 7.5

VERSION

  • 0.1

SYNOPSIS

omniJS code (see www.omni-automation.com) is executed in OmniGraffle Test 7.5.
The plugin returns a (JSON) string version of any value following the JS return keyword at the end of the omniJS script.
Read-only use of KM variables can be made inside the script (in the document.kmvar.VariableName format used with KM's Execute JS in Safari plugin), as long as the variables to be used have been named in the lower panel of the plugin. (One line per KM variable name, or comma-separated, without quotation).

NOTES

The context in which the omniJS code is run is that of the front OmniGraffle document.

Internally, the plugin executes JS code in two quite separate JS Contexts: OmniGraffle JS, and JavaScript for Automation (JXA).
JXA is used to prepare and 'open' an omniJS URL from a stringified version of the omniJS user code, including any specific KM variables that are to be exported into that code. It is also used to harvest (and return to Keyboard Maestro) any return value left by the omniJS code in the custom data of the OmniGraffle document background. This is an interim approach to returning a value from omniJS. I understand that OmniGroup are preparing a more direct form of data exchange between omniJS and JXA;

EXAMPLE

To generate a table of all the elements in the OmniGraffle omniJS global namespace, try running the following omniJS code from the code panel of the plugin:

(Requires macOS Sierra onwards, and an OmniGraffle 7.5 Test build)

// listTable :: OG Canvas -> [String] -> OG Table
const listTable = (cnv, xs) => {
    const intSide = Math.ceil(Math.sqrt(xs.length));
    return Table.withRowsColumns(
        intSide, intSide,
        xs.map(x => Object.assign(cnv.newShape(), {
            textSize: 12,
            text: x,
            geometry: new Rect(0, 0, 80, 80),
        }))
    );
};

// table :: OG Table
const table = listTable(
    document.windows[0].selection.canvas,
    Object.getOwnPropertyNames(this)
);

return table.columns + ' * ' + table.rows;

REQUIREMENTS

  • Sierra onwards
    • The underlying script is written in Javascript for Applications using ES6 JS
  • A current build of OmniGraffle Test (7.5)
  • Keyboard Maestro 7/8 ?
    • This plugin should work with Keyboard Maestro 7, but has so far only been tested with Keyboard Maestro 8 test builds.

INSTALLATION

  • Drag the .zip file onto the Keyboard Maestro icon in the OS X toolbar.
  • (if updating a previous version of the action, first manually remove the previous copy from the custom actions folder)
    • ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions

CONTACT

2 Likes

For an example of using a Keyboard Maestro variable within the omniJS code, see:

https://forum.keyboardmaestro.com/t/paste-excel-or-tab-delimited-clipboard-to-omnigraffle-7-5-table?source_topic_id=7799
1 Like