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 Omni Automation scripts into URLs. Simply pass in the name of the Omni application to target, and the text of the script code.

Create Script URL

function encodeScriptForURL(OmniAppName, scriptCode){ var appName = OmniAppName.toLowerCase() var urlOpening = appName + "://localhost/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") encodedScript = encodedScript.split("_").join("%5F") encodedScript = encodedScript.split("*").join("%2A") encodedScript = encodedScript.split("-").join("%2D") return urlOpening + encodedScript }

Encode Script

As an example, the encoding routine is included in this webpage and connected to the form below so you can try it yourself. Paste your Omni Automation script into the field and click the Encode Script button and the script will be replaced with the encoded URL. Click the Run Script button and the encoded script will be executed.

Encode Argument

As detailed in the Script URL documentation, non-static scripts will require the use of the argument parameter in order to “approvable” for execution without user-interaction.

Encode Script Argument

function encodeArgumentForURL(argumentData){ var argumentString = JSON.stringify(argumentData) var encodedArgument = encodeURIComponent(argumentString) return "&arg=" + encodedArgument }

Enter the argument data to be used with your script URL in the field below. Click the Encode Argument button to replace the entered data with the encoded argument parameter, ready to be appended to your script URL:

NOTE: The optional script argument can be provided as a string, array, or object.

Argument (optional)

// string argument "How now brown cow." // array argument ["East", "South", "North", "West"] // object argument {"name":"My New Project", "notification":-3600, "dueDate":"Mon Apr 19 2021 09:30:00 GMT-0700 (PDT)"}