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

As an example, the encoding routine is included in this webpage and connected to the form below so you can try it yourself! Paste the “Blue Star” script example (shown above) 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.

NOTE: Detailed information and examples concerning the use of Omni Automation with HTML is available in the “Omni-Interactive HTML” section of this website.

Encoding and Running Omni Automation using Swift

According to Apple, Swift is a powerful and intuitive programming language for macOS, iOS, watchOS and tvOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast.

The following is an example of how to encode and run Omni Automation scripts using the Swift language:

AppleScriptObj-C is a powerful fusion of the AppleScript and Objective-C languages that enables standard AppleScript code to access macOS frameworks. The following example demonstrates the use of AppleScriptObj-C to execute an Omni Automation script copied to the clipboard:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
-- classes, constants, and enums used
property NSCharacterSet : a reference to current application's NSCharacterSet
property NSString : a reference to current application's NSString
try
display dialog "Execute the Omni Automation code on the clipboard with which application?" buttons {"Cancel", "OmniOutliner", "OmniGraffle"} default button 1 with icon 1
set applicationName to button returned of the result
if applicationName is "OmniOutliner" then
set appID to "com.omnigroup.OmniOutliner5"
else
set appID to "com.omnigroup.OmniGraffle7"
end if
set lowercaseAppName to my changeCaseOfText(applicationName, 1)
set clipboardScript to get the clipboard
set textWithErrorHandler to "try{" & clipboardScript & "}catch(err){console.log(err)}"
set encodedString to my encodeUsingPercentEncoding(textWithErrorHandler)
set the scriptURL to lowercaseAppName & "://localhost/omnijs-run?script=" & encodedString
tell application id appID
activate
tell current application
open location scriptURL
end tell
end tell
on error errorMessage number errorNumber
activate
if errorNumber is not -128 then
activate
display alert (errorNumber as string) message errorMessage
end if
end try
on encodeUsingPercentEncoding(sourceText)
-- create a Cocoa string from the passed AppleScript string, by calling the NSString class method stringWithString:
set the sourceString to NSString's stringWithString:sourceText
-- indicate the allowed characters that do not get encoded
set allowedCharacterSet to NSCharacterSet's alphanumericCharacterSet
-- apply the indicated transformation to the Cooca string
set the adjustedString to the sourceString's stringByAddingPercentEncodingWithAllowedCharacters:(allowedCharacterSet)
-- convert from Cocoa string to AppleScript string
return (adjustedString as string)
end encodeUsingPercentEncoding
on changeCaseOfText(sourceText, caseIndicator)
-- create a Cocoa string from the passed text, by calling the NSString class method stringWithString:
set the sourceString to NSString's stringWithString:sourceText
-- apply the indicated transformation to the Cocoa string
if the caseIndicator is 0 then
set the adjustedString to sourceString's uppercaseString()
else if the caseIndicator is 1 then
set the adjustedString to sourceString's lowercaseString()
else
set the adjustedString to sourceString's capitalizedString()
end if
-- convert from Cocoa string to AppleScript string
return (adjustedString as string)
end changeCaseOfText

Encode & Run Omni Automation using AppleScript

01

use AppleScript version "2.4" -- Yosemite (10.10) or later

02

use framework "Foundation"

03

use scripting additions

04

05

-- classes, constants, and enums used

06

property NSCharacterSet : a reference to current application's NSCharacterSet

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

DISCLAIMER

Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. OMNI-AUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. OMNI-AUTOMATION.COM provides this only as a convenience to our users. OMNI-AUTOMATION.COM has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and OMNI-AUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from OMNI-AUTOMATION.COM and that OMNI-AUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.