Action Forms

Action Forms allow the user to provide direct input and data to a script, so that it may use that information to better perform its designed automation functions.

To enable the use of provided data, forms are integrated into the standard Action PlugIn code, adding functions for the display and processing of the Form data.

When executed, integrated Forms provide a more seamless and customizable automation experience for the user.

example-action-form-interface

(⬆ see above ) Example action form in OmniGraffle for macOS

(⬇ see below ) Example action form in OmniGraffle for iOS

The following documentation details the coding structure of an Omni Automation Action containing user-interactive Action Forms.

Integrating a Form into an Action

Below is the JavaScript code for the example OmniGraffle action shown in the image and movie above. This action lets the user select the stroke type to be applied to the graphics selected in the frontmost document. This example script can be used as a template for creating interactive actions for all Omni Group applications that support Omni Automation.

The individual component functions are described in detail on other pages linked in the navigation column on the right side of this page.

 01-09  Action Metadata (required) indicates which Omni applications (targets) this action is designed to used with, as well as the text (label) used for the Automation menu item whose selection triggers the execution of the action.

 10-66  The main function that contains the action and integrated form functions.

// ACTION METADATA (REQUIRED) /*{ "type": "action", "targets": ["omnigraffle"], "author": "Otto Automator", "identifier": "com.omni-automation.og.stroke-type-form", "description": "Applies chosen stroke type to the selected graphics.", "label": "Stroke Type", "shortLabel": "Stroke Type" }*/ // MAIN FUNCTION (() => { // CREATE ACTION OBJECT var action = new PlugIn.Action(function(selection, sender){ // CONSTRUCT THE FORM var form = new Form() // CREATE FORM ELEMENTS firstStrokeType = StrokeType.all[0] // StrokeType['Single'] typeMenu = new Form.Field.Option( "strokeType", "Stroke Type", StrokeType.all, null, firstStrokeType ) // ADD ELEMENTS TO FORM form.addField(typeMenu) // SHOW THE FORM AND RETURN JAVASCRIPT PROMISE formPrompt = "Choose the stroke type for the selected graphics:" buttonTitle = "Continue" formPromise = form.show(formPrompt, buttonTitle) // VALIDATE FORM CONTENT form.validate = function(form){ return true } // PROCESS THE FORM RESULTS formPromise.then(function(form){ // RETRIEVE CHOSEN VAUES strokeTypeValue = form.values['strokeType'] // PERFORM TASKS selection.graphics.forEach(function(graphic){ graphic.strokeType = strokeTypeValue }) }) // PROCESS FORM CANCELLATION formPromise.catch(function(error){ console.log("form cancelled", error) }) }) // VALIDATE DOCUMENT SELECTION action.validate = function(selection, sender){ return (selection.graphics.length > 0 ? true:false) } // RETURN ACTION OBJECT return action }();

The following continues the description of the various components of the action plugin:

 12-61  A new instance of an action is created and stored in the variable: action

 15  A new instance of the Form class is created within the action function and is stored in the variable: form

 18-25  One or more form elements are created, in this case a menu object displaying a list of the available stroke types for an OmniGraffle graphic. In this example, the created form element is stored in the variable: typeMenu

 28  Form elements are added to the form using the addField(…) instance method which in this example takes as its parameter, the form element reference stored in the variable: typeMenu

 31, 32  The text of the form prompt and approval button title are stored in variables.

 33  The form is displayed to the user by calling the show(…) method and passing in the variables representing the prompt and button title as function parameters. The result of the form display is an instance of the standard JavaScript Promise object. The Promise object represents the eventual completion (or failure) of an asynchronous operation (displaying the form), and its resulting value. A reference to the promise is stored in the variable: formPromise

 36-38  The result of the validate instance function of the Form class determines the state of the form’s approval button. If a boolean value of true is returned, the approval button becomes enabled. Script statements for checking the validity of the user element selections and/or input are placed within this function.

 41-49  When the approval button has been selected by the user, the then(…) method of the stored Promise object is called, and is passed as its parameter the form values object, which contains the keys and their corresponding values for each of the form’s elements. Within this function, the user-chosen form element values are retrieved and any actions are performed using the retrieved data.

 52-56  Should the user have selected the Cancel button in the form dialog, a standard catch(…) method will be called on the stored Promise, allowing the script the opportunity to execute any necessary clean-up statements.

 59-61  The action object uses a validate(…) function to determine if the action’s menu item in the hosting application’s Automation menu should be enabled. For example, the code in this example returns a postive value (true) if one or more graphics are selected in the frontmost document.

 64  The action object, containing the integrated form and related functions, is returned to the script.

 66  The main function is called and the action executed.

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