Omni Automation macOS 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.
Encode Omni Automation for URL | ||
01 | function encodeForOmniJS(OmniAppName, scriptCode){ | |
02 | appName = OmniAppName.toLowerCase() | |
03 | urlOpening = appName + ":///omnijs-run?script=" | |
04 | var encodedScript = encodeURIComponent(scriptCode) | |
05 | encodedScript = encodedScript.split("'").join("%27") | |
06 | encodedScript = encodedScript.split("(").join("%28") | |
07 | encodedScript = encodedScript.split(")").join("%29") | |
08 | encodedScript = encodedScript.split("!").join("%21") | |
09 | encodedScript = encodedScript.split("~").join("%7E") | |
10 | encodedScript = encodedScript.split(".").join("%2E") | |
11 | return urlOpening + encodedScript | |
12 | } |
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.
To run the script, select the circle in the OmniGraffle document, and then 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:
Once approved by the user, the script will execute, changing the color values of the fill, stroke, and text.
Next Topic
Click the Attached URLs link in the navigation sidebar at the top right of this window.
This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.