Integrated Scripts and Expressions in Desktop Applications

User-Defined Codes in Developer-friendly Desktop Apps

Hrach Mkrtumyan
Picsart Academy
7 min readMay 10, 2022

--

In the history of desktop applications, the Graphical User Interface (GUI) has passed through a huge evolution. It has been refined with design, convenience, and functionality year by year to meet the needs of the application user.
Old computer interfaces look so scary now. It seemed like the only way to use a computer is to master a low-level programming language. Today’s technologies have encapsulated everything connected to machine-level operations and computer users have no idea what is going on in the backend when they give a particular command to the machine.
However, any kind of automation causes a loss of flexibility. Software engineering now tends to partially go back to the coding-like user interfaces to bring back the lost flexibility.

Most desktop applications, especially graphical and data-related ones provide a feature of integrated scripts and expressions.

The most famous utilization of this that you will have most likely encountered is the cell expressions in the program Microsoft Excel. Also, there is a scripting feature in Excel which is less popular.
Most graphical programs like Adobe Photoshop, Adobe After Effects, Cinema 4D, 3D Max, Unity, etc. also provide this feature.
Every platform has its specification with syntax. The one used in Adobe applications is similar to JavaScript but actually called ExtendScript. In Cinema4D python is applied. Unity, the most developer-friendly platform for graphical uses, supports C# and so.

Differences Between Script and Expression

The scripts consist of expressions. Both the script and the expression are supposed to execute instructions based on the input data they get.

But by contrast with scripts, expressions return some value as input to another expression or script. The expression can only read program data while a script can change, remove or add data to the program.

The script gives sense to the expression. If the returned value of an expression is not used anywhere, the expression is useless. For those who have a basic understanding of programming, the expression

is useless until the result of this expression is saved to the memory. Allocating memory and saving the returned value of the expression (9) in it is already a script.

However, in desktop applications with the integrated script and expression feature, the image is a little different. You can even save the result of the expression in memory and that can be still called an expression, not a script. That’s because the user-defined memory containers and objects are deleted automatically when the execution of the integrated code is complete. To give the expression a sense (to complete it to a script) the result of the expression must be saved in a program part non-deletable memory: program properties.

The program properties in which you can write data usually have their own expression input section. Here the user writes an expression and its return value is automatically assigned to that property. However a script is typed in a global editor in the program, and besides expressions, it must include an appropriate instruction pointing to the property path where the result of the script-part expressions must be saved.

When writing expressions in the expression section (in Excel you just type = and everything typed after it is considered an expression), there is no need to worry about assigning the data to the property cause the software engineers building these desktop applications have cared about that.
The user is only supposed to write a sensible expression and regard the syntax. The assignment of the expression’s result to the program’s appropriate property is outside of the user’s responsibility. For example in After Effects, in the expression section of one property, you can write several expressions and assign them to different variables. The output value of the expression is considered the return value of the last expression.

The return value of the last expression ( a + b) is the output of the whole code. The expression (a + a) is ignored. If we delete the last expression, the previous one will become the result. And that output result is assigned to the property (data container) in which this expression is typed.

Expression and Script Use

The most simple use of an expression in Excel is telling one cell to indicate the sum of other cells. This use automates the process of calculating the sum of maybe hundreds of cells every time one of their value is changed. We can apply conditional expressions, functions, etc., and get some global information like date, time, etc.

So expressions can only read program data. They can’t write data except for the unit in which the expression is typed.

But scripts can. In Excel, the user can record a series of instructions simply by clicking the record button. Every action the user then takes, (a button click, a keyboard press) the appropriate instruction on the particular property is automatically generated in the script file. Then it’s possible to open the file and make some editions in the script manually. It’s possible to write an algorithm with logic, say, clear all the cells containing odd numbers.

Similarly in After Effects, a motion graphics application, very huge optimizations through expressions and scripts can be applied. For example, there are 3 objects on a 2D screen and the user wants one object always to be in the middle of the second two. And that two objects are moving with a random path. To implement this animation 100% precisely without an expression not only is difficult but also impossible.

after effects expression
expression generated position coordinates keep the layer right in the middle of the other two.

In this simple animation example the path, the direction, and the speed of objects 1 and 2 are defined by the animator manually. But not those of the object 3. Neither was it possible to guess what pattern it would depict and at what speed.
Moreover, this is a very simple example. The paths of the manually defined animations are pure circles and the speeds are also round: one is two times faster than the other. That's why the path of the middle object turned out to be a unique pattern.
Usually, that’s not the case. Both animation paths and speeds almost always are random and at least so is the derived object’s one. The expression typed in the position property of the object 3 is something like the following:

As was mentioned above, the expression reads the position properties of the two objects for each unit of time and generates new positions. To increase the potential of the expression we can modify it like this.

Now we can assign a value to (t) within the range (0 – 1) and so define the position ratio of the object between the two.
There are some instructions provided as a script right from the application developers. It may seem odd why they didn’t make standard instructions (tools) like the other ones existing in the program (in the instructions menu). Like them, these script instructions exist once the program is installed and there is hardly a difference in their importance․

Sorting script. The layers are sorted by their starting points in time (inPoint)
Sorting script. The layers are sorted by their starting points in time (in Point)

So this is done symbolically by the software developers to indicate some examples of scripts. This sorting script contains an algorithm with not much difference from standard sorting algorithms with iterations. It just reads the starting point times from the layers (properties) and based on that, places every layer to its appropriate index (sorts).
In this example, the user saves time by not examining the timeline, the layer starting points pattern, and not doing 4 instructions: manually arranging the layers. However, with more complex projects, more time can be saved.

Sorting layers manually
Sorting layers manually

Building expressions and scripts do not save time immediately after the first attempt, so don’t get disappointed discovering that building one took more time than doing the task in a traditional way. A task may take 5 minutes to complete with a traditional way and 1 minute with expression/script but building the code may take you 40 minutes. So in this case the code turns out to compensate for its building time and justify its mission after being applied more than 10 times. The 11th time the user earns the first 4 minutes and so.
If it’s not clear that the task to be automated is applicable enough that it will compensate for its building time and it’s able to be done in a non-scripted way, it would rather not be automated.
The picture is more beautiful when getting ready-made codes as plugins or presets. It’s also possible to save code as a preset or build a plugin which is another wide topic.

If you liked the article, let me know with comments and claps if this topic is worth extending, covering topics like building your own presets and plugins, in various programs (I.e. macros, so-called in Excel). Or the plugins in the most developer-friendly graphical application Unity.

--

--