Omni Automation iOS Tutorial: Encoding Scripts

An exciting aspect of Omni Automation is its ability to be expressed as URLs. Omni Automation URLs can be used to communicate between applications in both macOS and iOS, may be “attached” to document objects, and may be embedded in HTML documentation and webpages as clickable links.

Encoding Scripts for URLs

JavaScript scripts are able to be included in URLs because they are encoded using a standard method called “percent encoding.” With percent encoding, spaces and special characters are expressed as the percent character (%) followed by a two-character string. For example, a space is percent encoded as %20, a quote mark is %22, and a carriage return is represented as %3D.

Percent encoding enables complex scripts to be useable in URLs. And fortunately, the ability to encode strings with percent encoding is part of the standard JavaScript architecture. Here is a function for encoding OmniJS scripts into URLs. Simply pass in the name of the Omni application to target, and the script code.

function encodeForOmniJS(OmniAppName, scriptCode){ appName = OmniAppName.toLowerCase() urlOpening = appName + ":///omnijs-run?script=" var encodedScript = encodeURIComponent(scriptCode) encodedScript = encodedScript.split("'").join("%27") encodedScript = encodedScript.split("(").join("%28") encodedScript = encodedScript.split(")").join("%29") encodedScript = encodedScript.split("!").join("%21") encodedScript = encodedScript.split("~").join("%7E") encodedScript = encodedScript.split(".").join("%2E") return urlOpening + encodedScript }

Encoding a Script

To demonstrate script encoding, the function and calls from the previous section have been placed in the following text field. Tap the Encode Script button to display the encoded version of the script.

omnigraffle:///omnijs-run?script=function%20randomRGBColor%28alphaValue%29%7B%0A%09redValue%20%3D%20Math%2Efloor%28Math%2Erandom%28%29%20*%20101%29%20*%20%2E01%0A%09greenValue%20%3D%20Math%2Efloor%28Math%2Erandom%28%29%20*%20101%29%20*%20%2E01%0A%09blueValue%20%3D%20Math%2Efloor%28Math%2Erandom%28%29%20*%20101%29%20*%20%2E01%0A%09return%20Color%2ERGB%28redValue%2C%20greenValue%2C%20blueValue%2C%20alphaValue%29%0A%7D%0Agraphic%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Esolids%5B0%5D%0Agraphic%2EfillColor%20%3D%20randomRGBColor%281%29%0Agraphic%2EstrokeColor%20%3D%20randomRGBColor%281%29%0Agraphic%2EtextColor%20%3D%20randomRGBColor%281%29

To run the script, tap the Open URL button to execute the script to change the fill, stroke, and text colors of the circle graphic in our OmniGraffle tutorial document.

Safari will display a dialog asking for confirmation to execute the script URL:

Open URL confirmation dialog

In OmniGraffle, a second confirmation window will appear, displaying the unencoded script code with buttons for either stopping or executing the script code:

Script confirmation dialog

Once approved by the user, the script will execute, changing the color values of the fill, stroke, and text.

Script result

Next Topic

Tap the Attached URLs link in the navigation sidebar at the top right of this window.

UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER