JavaScript for Automation Question

Hi all,

I’d like to convert this tiny AppleScript to JavaScript :

tell application id “com.omnigroup.OmniGraffle6”
tell canvas of front window
make new shape at end of graphics with properties {size: {40.0, 30.0}, text: {size: 16, alignment: center, font: “HelveticaNeue”, text: “Toto”}, origin: {100.0, 100.0}}
end tell
end tell

Any idea ?

I’ve tried the following but this doesn’t work :

var app = Application(‘OmniGraffle’)

var t = app.Text({size:16, alignment:“center”, font:“HelveticaNeue”}, ‘Toto’)
var shape = app.Shape({size:[40,30], text:t, origin:[100,100]})
app.documents[0].canvases[0].graphics.push(shape)

I’ve also tried this but it doesn’t work either :

var t = app.Text({size:16, alignment:“center”, font:“HelveticaNeue”}, ‘Toto’)
var shape = app.Shape({size:[40,30], text:“Toto”, origin:[100,100]})
shape.text.size = 16
shape.text.font = “HelveticaNeue”
shape.text.alignment = “center”
app.documents[0].canvases[0].graphics.push(shape)

Any help would be appreciated.

Thanks,
Aurelien

Hi @rlbk were you able to figure this out? I have the same type of problem and would appreciate any pointers. Ingo

Essentially you have to push a new object into the graphics collection.

There are probably more efficient idioms/routes at various points but this kind of thing seems to work:


function run () {
    
    var og = Application('OmniGraffle'),
        ws = og.windows,
        w = ws.length ? ws[0] : undefined;

    if (w) {

        var shp = og.Shape({
            origin: [100, 100],
            size: [40, 30],
            text: "Toto"
        });

        w.canvas.graphics.push(shp);

        var refText = shp.text;

        refText.font = "HelveticaNeue";
        refText.size = 16;
        refText.color = [0, 0, 0];
        
        refText.attributeRuns[0].alignment = 'center';
    }
}

Or, perhaps a little more coherently:

function run() {
    'use strict'

    var og = Application('OmniGraffle'),
        ws = og.windows,
        w = ws.length ? ws[0] : undefined, // document window ?
        graphics = (w && w.id() !== -1 ? w.canvas.graphics : undefined);

    if (graphics) {

        var shp = og.Shape({
                origin: [100, 100],
                size: [150, 150]
            }),
            textRuns = (
                // push new shape into graphics collection
                graphics.push(shp),
                // and get a reference to the attribute runs of the text
                shp.text.attributeRuns
            );

        // Push a first attribute run
        textRuns.push(og.Text({
            size: 16,
            alignment: "center",
            color: [
                0,
                0,
                0
            ],
            font: "HelveticaNeue",
            text: "Toto"
        }));

        // and then a second attribute run,
        // with different formatting.
        textRuns.push(og.Text({
            size: 32,
            color: [
                1,
                0,
                0
            ],
            font: "Courier",
            text: "Toto"
        }));
    }
}

See also:

Thank you, @draft8 ! This worked flawlessly for me!

1 Like