OmniJS Copy as JavaScript loses magnets (are they scriptable yet ?)

Are magnets scriptable yet ?

I notice that Copy as JavaScript is dropping them, and I haven’t yet found syntax for successfully setting them.

(This works in AppleScript/JXA with a list of [x, y] pairs, and I have tried in OmniJS both with that data format and with an array of {x:1, y:0} etc dictionaries, but so far either I haven’t yet found the trick, or magnets are not yet scriptable.

(It looks as if they are read-only in OmniJS, at least for the moment,

// Error: The property "magnets" is read-only. undefined:21

but I am finding it hard to read them too)


Scripting magnets is the first thing that one needs in order to rotate a vertical tree into a horizontal tree.

Also, a script-created tree whose nodes lack magnets is a little too drunken and disorderly for easy legibility - especially with orthogonal links:

For refce, in JavaScript for Automation:

// Setting magnets in JavaScript for Automation
(() => {
    'use strict';

        og = Application("OmniGraffle"),
        ds = og.documents,
        d = ds.length > 0 ? ds[0] : undefined;
    if (d) {
            cnvs = d.canvases(),
            cnv = cnvs.length ? cnvs[0] : undefined,
            shps = cnv ? cnv.shapes : [];

        // WRITE
        shps().forEach(function(x) {      // Give North South magnets to all shapes
            x.magnets = [[0, 1], [0, -1]]

Sorry about this!

There was a bug in the JavaScript magnet implementation, which is fixed as of revision 283343, so setting/getting/copy as Javascript should all work correctly with magnets as soon as a new build goes out.

1 Like

Thanks, Greg – that sounds good

In other news, we seem to be able, at present, to reliably crash OG7 with an omniJS script if the list of graphic properties specified when a Graphic is created includes magnet settings like:

magnets: [[0,1], [0,-1]]

Which seems to be suggested by the omniJS documents description of them as Array of Array

A console test seems, however, to suggest a different type – an Array of some other objects:

Any idea what a correct magnet-setting syntax might look like at the moment ?

Got it … I was being slow:

(Not sure if there is a way of forestalling a crash when we use the wrong syntax, but clearly better to use the right syntax :-)

In the omniJS API Docs, perhaps this could be called an ‘Array of Point(s)’, rather than an ‘Array of Array’ ?

Creating a new shape by passing settings like this to the constructor now works flawlessly …

  const shapeDefaults = {
        textSize: 12,
        strokeType: null,
        strokeColor: null,
        shadowFuzziness: 7,
        cornerRadius: 9,
        magnets: [new Point(0, -1), new Point(0 ,1)]

Thanks !!

Thanks for this! I passed it along and one of our other engineers fixed these issues today. The API Reference should show “Array of Points” and if you pass the wrong thing it won’t crash any more, just return a JS error as of r290358.

1 Like

Perfect. Thanks !