In this section we’ll examine how to create and use functions in Omni Automation scripts, and how to convert scripts into encoded URLs that can be used to embed and execute scripts.
Functions
Omni Automation is based upon JavaScript Core and constructs of that implementation apply to Omni Automation as well. A JavaScript function is a block of code designed to perform a particular task, and is executed when it is “called” or invoked.
In this example, we’ll create a function for generating a random color value for setting the fill color, stroke color, and text color of a graphic solid.
1 Begin Function • Functions begin with the word function, followed by the name of the function, and any parameters to pass into the function code. In this example, we’ll include a pass-thru variable named alphaValue that will be the alpha value (0-1) of the created color instance.
2 Get Red Value • The Red value will be a number between 0 (inclusive) and 1 (inclusive), generated by two Math routines: random and floor
3 Get Green Value • By default, the random() method of the Math class generates a number between 0 (inclusive) and 1 (exclusive). To expand the method to optionally return a value of 1 (which would mean all of a color value), we expand to Math.random() * 101 which return a value from 0 to 100.99999 etc.
4 Get Blue Value • The floor() method of the Math class rounds down the provided value to the next integer, so 100.99999 will become 100. The calculation of multiplying the result time .01 reduces values from 0 to 100, to be values from 0 to 1. For example, 100 * .01 = 1
5 Return Color Instance • An instance of a RGB Color is created (using the passed alpha value) and the new color instance is returned as the result of the function.
6 End Function • The closing brace of the function.
Let’s use the function to change the color attributes of multiple properties of the circle graphic.
DO THIS ►
nbsp;Enter and run the following script that calls the function to change the fill color of the circle to a random color:
g1.fillColor = randomRGBColor(1)
Random Fill Color
01
g1.fillColor = randomRGBColor(1)
DO THIS ►
Enter and run the following script that calls the function to change the stroke color of the circle to a random color:
g1.strokeColor = randomRGBColor(1)
Random Stroke Color
01
g1.strokeColor = randomRGBColor(1)
DO THIS ►
Enter and run the following script that calls the function to change the fill color of the circle to a random color:
g1.textColor = randomRGBColor(1)
Random Text Color
01
g1.textColor = randomRGBColor(1)
Here is the function, and function “calls” entered in the console:
Here is the result of the random color function on the circle:
Next Topic
Tap Encoding Scripts link in the navigation sidebar at the top right of this 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.