Using Omni Automation with JXA and AppleScript

In macOS, Omni Automation scripts can be executed from within JavaScript for Automation (JXA) and AppleScript scripts and applets. Use the macOS Script Editor application to create and edit scripts in those languages.

NOTE: From within the Script Editor application in macOS, both AppleScript and JXA scripts can be saved as applets and script files, and can be run from the macOS system-wide Script Menu, and by third-party utilities like Hazel and FastScripts.

Encode Omni Automation Scripts

To encode your Omni Automation scripts into Omni Automation URLs, paste your script into the following field and TAP|CLICK one of the “Encode” buttons:


Once you have converted your Omni Automation script into an Omni Automation URL, the encoded URL code can be copied and inserted into either the JXA or AppleScript templates (provided below), automatically opened in the Script Editor application.

JavaScript for Automation (JXA)

JavaScript for Automation (JXA) is an OSA (Open Scripting Architecture) implementation of JavaScript Core in macOS. Introduced in OS X Yosemite, JXA is a peer of the AppleScript language and as such has access to all macOS scriptable apps, frameworks, and native UNIX utilities. Here are links to the JXA Release Notes and a 23-minute training video.

Since both Omni Automation and JXA are based upon JavaScript Core, you can easily create JXA scripts that run Omni Automation scripts. This accomplished using either of two techniques:

Here is an example of running an Omni Automation URL from within JXA:

app = Application.currentApplication() app.includeStandardAdditions = true var OmniAutomationURL = "replace-with-Omni-Automation-URL" app.openLocation(OmniAutomationURL)

And a template for encoding and running an Omni Automation script from within JXA. Note that the Omni Automation code to run is placed in a function (lines 10-17):

app = Application.currentApplication() app.includeStandardAdditions = true var OmniAutomationScript = "Omni Automation script goes here with new line characters \n" var OmniAutomationURL = encodeForOmniAutomation('Omni Application Name', OmniAutomationScript) app.openLocation(OmniAutomationURL) function encodeForOmniAutomation(OmniAppName, scriptCode){ appName = OmniAppName.toLowerCase() 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") return urlOpening + encodedScript }
// JXA execution code app = Application.currentApplication() app.includeStandardAdditions = true var scriptAsString = OmniAutomationScript.toString() + '\n' + 'OmniAutomationScript()' var OmniAutomationURL = encodeForOmniAutomation('Name of Omni Application', scriptAsString) app.openLocation(OmniAutomationURL) // Omni Automation script written as a function function OmniAutomationScript(){ // Omni Automation script goes here } // JavaScript encoding routine function encodeForOmniAutomation(OmniAppName, scriptCode){ appName = OmniAppName.toLowerCase() 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") return urlOpening + encodedScript }

AppleScript

The Omni suite of apps all support AppleScript and have AppleScript dictionaries. In addition, each application supports the evaluate javascript command for executing Omni Automation scripts from within AppleScript scripts.

tell application "OmniPlan" evaluate javascript "new Alert('My Title', 'My message.').show()" end tell

Omni Automation URLs can be executed from within AppleScript scripts by executing them using the open location scripting addition:

open location "encoded Omni Automation URL goes here"
applescript://com.apple.scripteditor?action=new&script=open%20location%20%E2%80%9Cencoded%20Omni%20Automation%20URL%20goes%20here%E2%80%9D

BBEdit

Add this AppleScript script to the BBEdit script menu, and you can execute Omni Automation script snippets selected in the frontmost BBEdit document. NOTE: Omni Automation errors that occur during script execution will be logged to the Omni application’s console 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