This guide has been written using AI. This warning will be removed once its contents have been checked over by a human.
Script Writing in LiveCode Create
This guide explains how to get started with script writing in LiveCode Create, where to write your scripts, and how to integrate scripting with visual workflows.
What is LiveCode Script?
LiveCode Script is a high-level programming language known for its English-like syntax. It is simple to read and write, making it ideal for programming beginners, yet it provides powerful functionality for app development.
Key Benefits of LiveCode Script:
- Easy to Learn: No complex symbols—commands and expressions are written in plain English.
- Powerful: Automate processes, manage data, and create custom behaviors.
- Flexible: Combine scripting with Actions to enhance workflows.
Example: A simple script to show a message when a button is clicked:
on mouseUp
answer "Hello, LiveCode Create!"
end mouseUp
Accessing the Script Editor
The Script Editor is where you write and edit LiveCode Script. To access it:
-
Using the Script Editor icon:
- Select the object you with to edit in the Canvas Area
- Click the Script Editor icon along the bottom of the app
-
By Right-Clicking a Control:
- Right-click any widget in the Canvas Area.
- Select Edit Script from the context menu.
-
From the Project Browser:
- Right-click a widget, layout, or object's item in the Project Browser.
- Select Edit Script.
-
Using the Message Box:
- Open the Message Box and type:
edit the script of [control]
Replace [control]
with the control’s reference, like button "SubmitButton"
.
Features of the Script Editor
The Script Editor provides tools to make script writing simple and efficient:
1. Code Writing Area
Write and edit your LiveCode Script here. It includes:
- Syntax Highlighting: Automatically color-codes keywords, functions, and variables for readability.
- Line Numbers: Navigate and reference lines of code easily.
- Auto-Completion: Speeds up writing by suggesting common commands, functions, and variable names.
- Auto-Indentation: Keeps your code neat and structured.
2. Error Highlighting
The Script Editor highlights syntax errors in real-time to help you identify and fix issues quickly.
3. Run and Test
Save and test your scripts directly in the IDE by switching to Run Mode. This allows you to see the effects of your script immediately.
Writing Your First Script
Here’s how to write a simple script in LiveCode Create:
Step 1: Select the Object
- Add a Button Widget to your layout.
- Right-click the button and select Edit Script.
Step 2: Write the Script
In the Script Editor, enter the following code:
on mouseUp
answer "Button Clicked!"
end mouseUp
on mouseUp
: The event that triggers when the button is clicked.answer
: Displays a message to the user.
Step 3: Save and Test
- Click Save to save your script.
- Switch to Run Mode.
- Click the button to see the script in action.
Mixing Script with Actions
LiveCode Create allows you to use both visual Actions and custom script seamlessly:
Using Raw LiveCode Script Blocks
In the Workflow Editor, you can add Raw LiveCode Script Blocks to insert custom code directly into your workflow:
- Open the Workflow Editor.
- Drag a Raw LiveCode Script Block from the Action Library.
- Write your script inside the block:
put "Welcome to LiveCode Create!" into field "WelcomeMessage"
Viewing Script for Actions
Every Action Block in the Workflow Editor generates LiveCode Script automatically.
- Expand Blocks: Click the Expand icon to see the generated script.
- Toggle Script View: Switch to Script Mode to view all the script for the workflow.
Unlocking Actions:
- In Script View, click the Padlock Icon to unlock an Action Block.
- The block becomes a Raw LiveCode Script Block, allowing you to modify the script.
Example: Customizing a Workflow with Script
Imagine you want to navigate to a layout and show a dynamic message:
-
In the Workflow Editor:
- Drag the Navigate to Layout Action.
- Drag a Raw LiveCode Script Block after it.
-
Add Custom Script:
go to layout "Dashboard"
answer "Welcome to the Dashboard!"
- Save and test the workflow to see the result.
Advanced Script Features
Variables and Data Handling
You can use variables to store and manipulate data in LiveCode Script:
local tName
put "John Doe" into tName
answer "Hello, " & tName
Loops and Conditionals
Add logic to your scripts using if/else statements and loops:
repeat with i = 1 to 5
put "Item " & i into line i of field "ListField"
end repeat
Best Practices for Script Writing
- Start Small: Begin with simple scripts and test them frequently.
- Comment Your Code: Use comments to explain what your script does.
# This script shows a welcome message
on openCard
answer "Welcome to my app!"
end openCard
- Use Descriptive Names: Name variables and objects clearly for readability.
- Combine Actions and Script: Use Actions for common tasks and script for advanced behaviors.
- Test Often: Test scripts in Run Mode to ensure they work as expected.
For further learning: