Text Input Fields (Form.Field.String)

A fundamental mechanism for prompting for and retrieving textual data from a script user, is the use of the text input field. Omni Automation action forms provide this form element to enable the passing of textual data to the script for processing.

textInputField = new Form.Field.String( "textInputField", null, null)

 1  Instances of the form string input element are created using the new constructor with provided parameters that include the identifying key, an optional menu label (title), and an optional default string value or the field. The result of the constructor is a new instance that is stored in the variable: textFieldInput

 2  The string input is assigned a unique identifying key (string) that is used to extract the current displayed text input object when the form is validated or processed.

 3  The optional text to use as the label for the menu.

 X  An optional default string to be used as the value for the input field.

On iOS systems only, there is an optional property of the Form.Field.String class for setting which keyboard is displayed on the device (iPhone | iPod Touch):

Class properties of the KeyboardType class:

Using the Device class to determine whether or not to set the keyboard type:

if (Device.current.type === DeviceType.iPhone){ textInputField.keyboardType = KeyboardType.PhonePad }

Simple Example Console Script

The following is an example of to create, display, and process the results of a form with a single text input field. As the script example is not placed within an Omni Automation action framework, this type of script is best run from either the console or a script URL.

simple-text-input-dialog
textInputField = new Form.Field.String( "textInput", "ID", "PCODE-XXXX-MM.DD.YYYY" ) var inputForm = new Form() inputForm.addField(textInputField) inputForm.show("Please enter the project identifier:","Continue") .then( function(formObject){ console.log(formObject.values['textInput']) } )
omnigraffle://localhost/omnijs-run?script=try%7BtextInputField%20%3D%20new%20Form%2EField%2EString%28%0A%09%22textInput%22%2C%0A%09%22ID%22%2C%0A%09%22PCODE%2DXXXX%2DMM%2EDD%2EYYYY%22%0A%29%0Avar%20inputForm%20%3D%20new%20Form%28%29%0AinputForm%2EaddField%28textInputField%29%0AinputForm%2Eshow%28%22Please%20enter%20the%20project%20identifier%3A%22%2C%22Continue%22%29%0A%2Ethen%28%0A%09function%28formObject%29%7B%0A%09%09console%2Elog%28formObject%2Evalues%5B%27textInput%27%5D%29%0A%09%7D%0A%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

 01-05  The creation of a new instance of the Form.Field.String (text input) class. Note the inclusion of the identifying key string "textInput" that is used elsewhere in the script to retrieve the current input value of the form element.

 06  A new empty instance of the Form class is created and stored in the variable: inputForm

 07  The created text input field is added to the form using the addField(…) method of the Form class.

 08  The form is displayed to the user by passing string values for the dialog prompt and button title into the show(…) method called on the created form instance. Note that the result of the form display is a JavaScript Promise object which will contain the form results when the form dialog has either been approved or declined by the user.

 09-13  Although no representation for the JavaScript Promise is displayed, the then(…) method is called on the implied promise resulting from the previous show(…) method. Placing .then(…) on a new line is simply a connivence for displaying the code. Functionally, it is the same as appending to the previous method, like this: inputForm.show(…).then(…)

 10  The callback function within the then(…) method is passed the form object as its parameter.

 11  Using the text input’s identifying key, the current input value of the field is retrieved from the record returned as the value of the passed form’s values property. Any processing of the retrieved data by the script would be placed in the scrpt statements following this one.

Example Action: Integer Input

The following Omni Automation action for OmniGraffle includes the display, validation, and processing of a form containing a single text input field. In this example, the field is designed to accept only integers occurring within a specified range.

integer-input
/*{ "type": "action", "targets": ["omnigraffle"], "author": "Otto Automator", "identifier": "com.omni-automation.og.integer-input", "description": "Displays a form with a field for accepting an integer value within a specified range.", "version": "1.1", "label": "Integer Input", "shortLabel": "Integer" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // CONSTRUCT THE FORM var inputForm = new Form() // CREATE FORM ELEMENTS: TEXT INPUT, DATE INPUT, AND CHECKBOX textField = new Form.Field.String("input", "(1 to 34)", null) // ADD THE ELEMENTS TO THE FORM inputForm.addField(textField) // DIALOG PROMPT AND OK BUTTON TITLE let formPrompt = "Enter the number of segments to add:" let buttonTitle = "Continue" // DISPLAY THE FORM DIALOG formPromise = inputForm.show(formPrompt, buttonTitle) // VALIDATE FORM CONTENT inputForm.validate = function(formObject){ // EXTRACT VALUES FROM THE FORM’S VALUES OBJECT textValue = formObject.values['input'] if(!textValue){return false} intValue = parseInt(textValue) validation = (isNaN(intValue) || intValue > 34 || intValue < 1) ? false:true // RETURN THE VALIDATION STATUS return validation } // PERFORM PROCESSES USING FORM DATA formPromise.then(function(formObject){ textValue = formObject.values['input'] intValue = parseInt(textValue) console.log(intValue) // PROCESSING STATEMENTS GO HERE }) // PROCESS FORM CANCELLATION formPromise.catch(function(error){ console.log("form cancelled", error) }) }); return action; })();

 01-09  The required metadata header for the action file.

 10-60  The creation and execution of an Omni Automation Action Plug-In.

 14  Place a new empty instance of the Form class into the variable: inputForm

 17  Place a new instance of a text input field into the variable: textField

 20  Use the addField(…) method of the Form class to add the created field into the created form.

 23,24  Place the form dialog prompt and approval button title into variables.

 27  Pass the prompt and button title into the show(…) method of the Form class to display the form dialog to the user. The result of the dialog display will be a JavaScript Promise object that is stored in the variable: formPromise

 30-42  The validation function for the displayed form. This is called when the dialog is displayed, and every time the form settings are edited by the user. The function is passed a reference to the form object in the parameter: formObject

 32  The current value (text input) of the text field is extracted from the passed form by retrieving the value of the form’s values property, which is a JavaScript object containing the key/value pairs for each form element. The key "input" is used to retrieve the value of the text input field.

 34  If the field contains no text, thereby returning no value, return a boolean value of false from the validation function, thereby keeping the approval button disabled.

 36  If a string is entered in the text input field, attempt to convert it into an integer value using JavaScript’s parseInt(…) method, storing the resulting value in the variable: intValue

 38  This ternary statement returns a value of false if: the entered string could not be converted into an integer value; or the provided integer value is greater than a specified number; or the provided integer value is less than a specified amount. However, if the provided input is an integer withi8n the specified range, then a value of true is returned from the the validation function, thereby enabling the form dialog’s approval button.

 41  Return the boolean validation value.

 45-51  The promise processing function then(…) is called when the form dialog has been approved by the user. A reference to the completed form object is provided as this function’s input parameter.

 46  The final value (text input) of the text field is extracted from the passed form by retrieving the value of the form’s values property, which is a JavaScript object containing the key/value pairs for each form element. The key "input" is used to retrieve the value of the text input field.

 47  The retrieve string is converted into an integer, which is then used in any processing routines added into this function.

 54-56  Should the user cancel the form dialog, the catch(…) function is called for the promise object, allowing the script the option to perform any clean up or other necessary statements.

 59  The action object is return to the enclosing function which gets called on the next line ().

DISCLAIMER