Best way to pass parameters to omnijs from Workflow.app

Hi,

I’m investigating writing a simple script for OmniOutliner on iOS. I would like to be able share some text to OmniOutliner and add that text to the beginning of a particular document.

Is there a way to pass parameters with omnioutliner:///omnijs-run?script=prependToOutline.js

Or do I need to be creating the JavaScript (with the parameter filled in) in workflow and then passing that to OmniOutliner?

So in simple terms, can I pass parameters to a script via URLs or should I create a custom script with the parameters coded into it?

I guess this is a lot down to my lack of knowledge of the OnniOutliner URL scheme. Is there documentation somewhere?

Thanks

Martin

I don’t think you can pass parameters in a OmniJS URL right now on. But, in this link, there are instructions about executing OmniJS scripts from Workflow app.

Omni Automation and Workflow App

1 Like

Fortunately, however, we can also launch omniJS URLs from 1Writer (@1WriterApp) or Drafts (@AgileTortoise), both of which (unlike Workflow.app) have:

  1. JavaScript interpreters, with
  2. app.openURL() methods.

This enables us to pass parameters by using the pattern which appears at the end of the source script in this example:

Essentially, in the Javascript interpreter of either of these apps, you

  1. Define your JS code as a function with an argument,
  2. convert the function to a string with the standard JS Function.toString() method and convert the argument to a string with the standard JSON.stringify())
  3. Use encodeURIComponent() to URL-ify the function and the options/parameters that you are passing to it.
(() => {
    'use strict';

    // DEFINE A FUNCTION
    const myFunction = (options) => {
        // ... create something in OmniGraffle using the keys and values
        // of the options dictionary here:

        console.log(options.alpha);
        console.log(options.beta);
    };

    // Place options in a dictionary
    const dctOptions = {
        alpha: 5,
        beta: 7
    };

    // CONVERT A FUNCTION CALL WITH PARTICULAR OPTIONS
    // TO A URL-ENCODED OMNIJS STRING

    const strURL = 'omnigraffle:///omnijs-run?script=' +
        encodeURIComponent(
            '(' + myFunction.toString() + ')(' +
            (dctOptions && Object.keys(dctOptions)
                .length > 0 ? JSON.stringify(dctOptions) : '') + ')'
        );

    // Open the omniJS url for this function call with an options parameter:

    // ( 1Writer and Drafts 5 both have an app.openURL() method,
    //   and they also both have an app.setClipboard() method )

    app.setClipboard(strURL);
    app.openURL(strURL);
})();
3 Likes