You can bundle the submission of JS functions by [1Writer, Drafts]
for evaluation in [OmniOutliner, OmniGraffle]
by writing a more general function to cover all those permutations, and any number of optional arguments, as in:
function omniJSFunctionWithArgs(targetAppName, f) {
const args = Array.from(arguments);
app.openURL(
targetAppName.toLowerCase() +
'://localhost/omnijs-run?script=' +
encodeURIComponent(
'(' + f + ')' +
'(' +
args.slice(2)
.map(JSON.stringify)
.join(', ') +
')'
)
);
};
So, for example, if we not only want to fill an OG shape with text from 1Writer or Drafts, but also want to provide any number of additional arguments, specifying, perhaps, font and font size, for a start:
(() => { // 'Module' wrapping
// (in immediately invoked anonymous function)
// prevents clashes in the global name-space,
// particularly between successive script actions.
'use strict' // This provides richer and more informative
// error messages.
// OMNIGRAFFLE JS Context
// textForSelectedGraphic :: String -> OG IO()
const textForSelectedGraphic = (txt, fontName, fontSize) => {
try {
g = document.windows[0].selection.graphics[0];
g.text = txt;
g.fontName = fontName;
g.textSize = fontSize;
} catch (err) {
console.log(err)
}
};
// OTHER JSContext ( 1Writer or Drafts )
function omniJSFunctionWithArgs(targetAppName, f) {
const args = Array.from(arguments);
app.openURL(
targetAppName.toLowerCase() +
'://localhost/omnijs-run?script=' +
encodeURIComponent(
'(' + f + ')' +
'(' +
args.slice(2)
.map(JSON.stringify)
.join(', ') +
')'
)
);
};
// Unlike `drafts.content`, editor.getText()
// works in both 1Writer and Drafts
const strText = editor.getText();
omniJSFunctionWithArgs(
'omnigraffle',
textForSelectedGraphic,
// And as many arguments as the OO/OG function needs:
strText,
'Courier',
20
);
})();
or rearranging, if you prefer:
(() => {
'use strict'
const main = () => {
// 1WRITER OR DRAFTS CONTEXT
const strText = editor.getText();
// Submitting a function to Omnigraffle
// with any arguments needed
omniJSFunctionWithArgs(
'omnigraffle',
textForSelectedGraphic,
{
text: strText,
fontName: 'Courier',
textSize: 24
}
);
};
// OMNIGRAFFLE JS Context
// textForSelectedGraphic :: String -> OG IO()
const textForSelectedGraphic = options => {
try {
Object.assign(
document.windows[0].selection.graphics[0],
options
)
} catch (err) {
console.log(err)
}
};
// Mechanics for other JSContext ( 1Writer or Drafts )
function omniJSFunctionWithArgs(omniName, f) {
var args = Array.from(arguments);
app.openURL(omniName.toLowerCase() +
"://localhost/omnijs-run?script=" +
encodeURIComponent("(" + f + ")(" +
args.slice(2).map(JSON.stringify)
.join(", ") + ")"));
};
// MAIN ---
return main();
})();