Scripts with JS

Script with JavaScript

Scripts can provide a powerful way to enhance your app's functionality and customize its behavior beyond what can be achieved with declarative configurations alone.

When to Use Scripts

Global Scripts are ideal for situations where you need:

  • Reusable Logic: Define functions or variables that can be used across multiple screens or widgets in your app. For example, a function to format dates or calculate totals could be placed in a Global Script for easy access.
  • Data Storage: Store pre-defined data like user details, color palettes, or API endpoints in a Global Script for consistent use throughout your app. This simplifies updates and avoids code duplication.
  • Custom Logic: Scripts allow you to Implement complex logic, manipulate data, perform calculations, and interact with external services and APIs.

How to Create a Script

In Ensemble Studio, navigate to your app and select Scripts from the left menu. Click the button Create new script present at the top right corner of the studio.

add language

Now open the script file and you can use standard JavaScript syntax to define variables and functions and then save the file.

add language

Ensemble currently supports JavaScript syntax up to ES5.

How to Import the script

In the YAML code for your screen or widget, use the Import section to reference the script file. For example:

  - Common

How to Use the Script

The code example shows how to access variables and functions defined in the Global Script from your screen's YAML code.

  - Common
    backgroundColor: ${colors.snowGrey} # Here we have used color imported from the Common Script.
    title: Global scripts
        padding: 24
        gap: 8
        - Button:
          label: Run function from Common
            backgroundColor: ${} # Here we have used color imported from the Common Script.
              body: |
                // Import the getUserFromScript function from the Common script
                var thisUser = getUserFromScript();
                // Now assigning text to the Fields
                nameText.text = "Name: " +;
                companyText.text = "Company: " +;
        - Column:
            - Text:
              text: User Details
                  color: ${}
                  fontSize: 21
                  fontWeight: bold
                  isItalic: true
            - Text:           # Display user's name
              id: nameText
            - Text:           #  Display user's company
              id: companyText


  1. Accessing Variables from the Script:
    • The backgroundColor of the view is set using ${colors.snowGrey}. This references the snowGrey variable defined in the colors object within the Common Script. Similarly, the backgroundColor of the button is set using ${}.
  2. Accessing Functions from the Script:
    • Clicking the button triggers the onTap event, which calls the executeCode block. Inside the block, the getUserFromScript() function (defined in Common Script) is called to retrieve user information.
    • The returned data is stored in the thisUser variable. To display the user's name, the code constructs a string by combining "Name: " with the name property of the thisUser object.