In the OmniGraffle 6 menu, Edit > Copy As offers AppleScript but not JavaScript for Automation.
Below is a first rough draft (please test and report where it fails) of a Copy As > JavaScript for Automation script.
Copy selected OmniGraffle 6 shapes as JavaScript for Automation
OVERVIEW
When OG6 shapes are selected, and the script is run, it places in the clipboard:
- A JavaScript Object representation of the shapes and their properties
- A JS function which takes the JSO as an argument, and creates the
shapes in the front canvas of the front OG6 document
USE
- Select graphic(s) in OmniGraffle 6
- Run this script (‘Copy As JSA’)
- Paste the updated clipboard into an empty Script Editor document,
- Choose
JavaScript
at top left of Script Editor, and run the generated code to regenerate the shapes
// Copy selected OmniGraffle 6 shapes as JavaScript for Automation
//
// ver 0.19
//
// **OVERVIEW**
//
// When OG6 shapes are selected, and the script is run, it places in the clipboard:
//
// 1. A JavaScript Object representation of the shapes and their properties
// 2. A JS function which takes the JSO as an argument, and creates the
// shapes in the front canvas of the front OG6 document
//
// **USE**
//
// 1. Select graphic(s) in OmniGraffle 6
// 2. Run this script ('Copy As JSA')
// 3. Paste the updated clipboard into Script Editor,
// 4. Choose `JavaScript` at top left of Script Editor, and run the generated code to regenerate the shapes
var strClip = (function () {
'use strict';
var propSet = function (g, ps) {
var ps = ps || g.properties(),
// meta-properties - not directly used for creation of graphic
mps = [
'canvas', 'class', 'destination', 'id', 'layer', 'source'
].reduce(function (a, k) {
var v = ps[k];
if (v) a[k] = v;
delete ps[k];
return a;
}, {}),
strClass = mps.class,
// class, id, idFrom, idTo, and other properties directly used in creating graphic
dctG = {
class: capitalised(strClass),
id: g.id()
.toString(),
props: Object.keys(ps)
.concat(['size', 'origin'])
.reduce(function (a, k) {
var v = ps[k],
kv = (typeof v === 'undefined') ? g[k]() :
(
k === 'text' ? textRuns(g[k]) : v
);
if (kv) a[k] = kv;
return a;
}, {})
},
fnFrom = mps.source,
fnTo = mps.destination;
// Source and destination ids for connecting lines
if (fnFrom) dctG.from = fnFrom()
.id()
.toString();
if (fnTo) dctG.to = fnTo()
.id()
.toString();
return dctG;
},
// Attribute runs for formatted text
textRuns = function (v) {
return (typeof v === 'string') ? [v] : (function () {
var rs = v.attributeRuns,
lng = rs.length,
i = rs.length,
lst = [],
tps; // text properties
for (i = 0; i < lng; i++) {
tps = rs.at(i)
.properties();
delete tps.class;
lst.push( // skipping 'falsy' values
Object.keys(tps)
.reduce(function (a, k) {
var v = tps[k];
if (v) a[k] = v;
return a;
}, {})
);
};
return lst;
})();
},
capitalised = function (s) {
return s.charAt(0)
.toUpperCase() + s.slice(1);
},
og = Application("OmniGraffle"),
ws = og.windows,
w = ws.length ? ws[0] : undefined,
selns = w ? w.selection() : [];
var dctLayers = selns.reduce(function (a, g) {
var k = g.layer.name(),
v = a[k],
dctProps = propSet(g);
// array of graphics for each layer
a[k] = v ? v.concat(dctProps) : [dctProps];
return a;
}, {});
return '(' + (function (dctLayers) {
'use strict';
// Creates layers and shapes defined at end,
// as arguments to this function
var og = Application('OmniGraffle'),
ds = og.documents,
d = ds.length ? ds[0] : (ds.push(og.Document()), ds[0]),
ws = og.windows,
w = ws.length ? ws[0] : undefined,
cnv = (w && w.id() !== -1) ? w.canvas() : undefined,
layerNames = Object.keys(dctLayers),
blnLayers = layerNames.length > 1,
lstLinked = [];
// CREATION of lines and shapes
layerNames.forEach(function (strName) {
if (blnLayers) {
cnv.layers.push(
og.Layer({
name: strName,
visible: true,
prints: true
})
)
}
var oLayer = cnv.layers[0],
lgs = oLayer.graphics,
dctNodes = {};
dctLayers[strName].forEach(function (g) {
var dctProps = g.props,
oText = dctProps.text,
lstRuns = oText ? (
oText instanceof Array ?
oText : [oText]
) : [],
oGraphic = og[g.class](
(
dctProps.text =
undefined,
dctProps
)
),
textRuns = (
lgs.push(oGraphic),
oGraphic.text.attributeRuns
),
strID = g.id.toString(),
strFrom = g.from,
strTo = g.to;
lstRuns.forEach(function (dctRun) {
textRuns.push(
og.Text(dctRun)
);
});
if (strFrom || strTo) lstLinked.push(
[strID, strFrom, strTo]
);
dctNodes[strID] = oGraphic;
});
// CONNECTION of lines and shapes
lstLinked.forEach(function (tuple) {
var g = dctNodes[tuple[0]],
strFrom = tuple[1],
strTo = tuple[2];
if (strFrom) g.source = dctNodes[
strFrom];
if (strTo) g.destination = dctNodes[
strTo];
});
});
})
.toString() + ')(' + JSON.stringify(dctLayers, null, 2) + ');';
})();
var a = Application.currentApplication(),
sa = (a.includeStandardAdditions = true, a);
sa.setTheClipboardTo(strClip);
strClip;