UPDATE
Ah … I should have used:
sa.openLocation(strURL);
(which executes the script immediately)
QUESTION WAS:
Do all URL launches inevitably need to pass through an ‘Allow’ dialog ?
I notice that if I run a URL from a shell command line, then it only works if we specify Safari as the opening application. e.g. (from JXA, creating and launching an OmniJS URL-encoded script), this works, after throwing up a Safari ‘Allow’ dialog:
sa.doShellScript("open -a Safari '" + strURL + "'");
whereas this more general open
activates OmniGraffle but seems to have no scripting effect:
sa.doShellScript("open '" + strURL + "'");
(Full test JXA -> OmniJS test script below):
(() => {
'use strict';
// renderNode :: {line, fill, shape, shadow} -> {text, geom} -> JS String
const renderNode = (dctStyle, dctNode, intID) => {
const
g = dctNode.geom,
id = 'g' + intID.toString(),
sh = dctStyle.shadowColor,
cnr = dctStyle.cornerRadius;
return 'var ' + id + intercalate(';\n' + id, [
' = canvas.newShape()',
'.text = "' + dctNode.text + '"',
'.cornerRadius = ' + (cnr !== undefined ? cnr.toString() : '0'),
'.textSize = ' + dctStyle.textSize.toString(),
'.shadowColor = ' + (isNull(sh) ? (
'null'
) : 'Color.RGB(' + intercalate(', ', sh) + ')'),
'.geometry = new ' + dctStyle.shape,
]) + '(' + [g.x, g.y, g.w, g.h].join(', ') + ');\n';
};
// renderNodes :: [Node] -> String
const renderNodes = (dctStyle, lstNodes) =>
zipWith(
curry(renderNode)(dctStyle),
lstNodes,
enumFromTo(1, lstNodes.length)
);
// GENERIC FUNCTIONS ------------------------------------------------------
// 2 or more arguments
// curry :: Function -> Function
const curry = (f, ...args) => {
const go = xs => xs.length >= f.length ? (f.apply(null, xs)) :
function () {
return go(xs.concat([].slice.apply(arguments)));
};
return go([].slice.call(args, 1));
};
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
// intercalate :: String -> [a] -> String
const intercalate = (s, xs) => xs.join(s);
// isNull :: [a] -> Bool
const isNull = xs => (xs instanceof Array) ? xs.length < 1 : undefined;
// min :: Ord a => a -> a -> a
const min = (a, b) => b < a ? b : a;
// show :: a -> String
const show = x => JSON.stringify(x); //, null, 2);
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = (f, xs, ys) =>
Array.from({
length: min(xs.length, ys.length)
}, (_, i) => f(xs[i], ys[i]));
// TEST -------------------------------------------------------------------
const dctStyle = {
textSize: 16,
shadowColor: [0, 0, 1],
shape: 'Rect',
cornerRadius : 9
};
const lstNodes = [{
text: "Taking",
geom: {
x: 100,
y: 100,
w: 100,
h: 100
}
}, {
text: "first",
geom: {
x: 250,
y: 100,
w: 100,
h: 100
}
}, {
text: "steps",
geom: {
x: 400,
y: 100,
w: 100,
h: 100
}
}];
const hdr = 'var canvas = document.windows[0].selection.canvas;\n';
const strScript = hdr + unlines(renderNodes(dctStyle, lstNodes));
const omniURLPrefix = 'omnigraffle:///omnijs-run?script=';
const strURL = omniURLPrefix + encodeURIComponent(strScript);
var a = Application.currentApplication(),
sa = (a.includeStandardAdditions = true, a);
// UPDATE: I should have used:
// sa.openLocation(strURL)
sa.doShellScript("open -a Safari '" + strURL + "'");
})();