Scripts Module (How-to Guide) walks you through configuring the Scripts module for custom automation and business logic. You'll create reusable classes, automated tasks, simple expressions, and integrate external libraries using C#, VB.NET, or Python.
Prerequisites:
- Basic programming knowledge (C#, VB.NET, or Python)
- Understanding of event-driven programming
- Tags configured for script interaction
In this page:
Configuration Workflow
- Create ScriptClasses - Build reusable function libraries
- Create ScriptTasks - Implement automated processes
- Add ScriptExpressions - Simple one-line actions
- Add ScriptReferences - Include external DLL libraries
- Test and Debug - Verify script execution
Step 1: Create ScriptClasses
ScriptClasses are libraries of reusable methods available throughout your solution.
Creating a Class
- Navigate to Scripts → Classes
- Click Plus icon
- Configure:
- Name: Class identifier
- Code: Language (C#, VB.NET, Python)
- Domain: Server or Client execution
- ClassContent: Methods (standard) or Namespace (advanced)
- Click OK
- Double-click to open Code Editor
Built-in Classes
Class | Purpose | Domain |
---|---|---|
ServerMain | Methods for all server tasks | Server |
ClientMain | Methods for all client displays | Client |
Example Class Code
<ac:structured-macro ac:name="warning"> ac:rich-text-body Important: ScriptClasses with Methods type contain only method definitions - NO class declaration wrapper </ac:rich-text-body> </ac:structured-macro>
csharp
// CORRECT - Methods directly, no class wrapper
public double CalculateEfficiency(double input, double output)
{
if (input == 0) return 0;
return (output / input) * 100;
}
public void LogEvent(string message)
{
@Alarm.AuditTrail.AddCustomMessage(message);
}
public string GetStatus(double value, double limit)
{
return value > limit ? "High" : "Normal";
}
Step 2: Create ScriptTasks
ScriptTasks execute code based on triggers or schedules.
Creating a Task
- Go to Scripts → Tasks
- Click Plus icon
- Configure:
- Name: Task identifier
- Code: Language selection
- Domain: Server (default) or Client
- Trigger: Execution condition
- Period: Time interval (if periodic)
- InitialState: Enabled or Disabled
- Click OK
- Double-click to edit code
Built-in Tasks
Task | Trigger | Purpose |
---|---|---|
ServerStartup | Solution starts | Initialize server resources |
ServerShutdown | Solution stops | Cleanup operations |
ClientStartup | Client connects | Setup client environment |
ClientShutdown | Client disconnects | Client cleanup |
Trigger Options
Trigger Type | Example | Use Case |
---|---|---|
Tag Change | @Tag.Temperature | React to value changes |
Period | 60000 (ms) | Run every minute |
Condition | @Tag.Level > 100 | Monitor thresholds |
Startup | Built-in | One-time initialization |
Example Task Code
<ac:structured-macro ac:name="warning"> ac:rich-text-body Important: ScriptTasks contain only the method body - NO function declarations or wrappers </ac:rich-text-body> </ac:structured-macro>
csharp
// CORRECT - Code directly, no method wrapper
// Task: MonitorProduction
// Trigger: @Tag.ProductionRun
if (@Tag.ProductionRun == 1)
{
@Tag.StartTime = DateTime.Now;
@Tag.BatchNumber = @Tag.BatchNumber + 1;
// Call class method
@Script.Class.ServerMain.LogEvent("Production Started");
}
else
{
@Tag.EndTime = DateTime.Now;
TimeSpan duration = @Tag.EndTime - @Tag.StartTime;
@Tag.BatchDuration = duration.TotalMinutes;
}
Step 3: Add ScriptExpressions
Expressions are single-line VB.NET statements for simple operations.
Creating an Expression
- Navigate to Scripts → Expressions
- Add new row with:
Property | Description | Example |
---|---|---|
Object | Target tag to receive result | Tag.Result |
Expression | VB.NET calculation/action | Tag.Input1 + Tag.Input2 |
Execution | When to run | OnChange |
Domain | Server or Client | Server |
Trigger | Optional trigger tag | Tag.Calculate |
Expressions Syntax
Expressions use VB.NET / C# syntax and don't require @ symbol for tags, as there is no local .NET. variables declared in the same context.
Expression Methods
IIF Method (all parameters evaluated):
vbnet
Tag.Status = IIF(Tag.Value > 100, "High", "Normal")
' Both "High" and "Normal" are evaluated
TIF Method (conditional evaluation - recommended):
vbnet
Tag.Result = TIF(Tag.Mode = 1, Script.Class.ServerMain.Method1(), Script.Class.ServerMain.Method2())
' Only executes Method1() OR Method2(), not both
Common Expression Examples
vbnet
' Mathematical
Tag.Average = (Tag.Value1 + Tag.Value2) / 2
' Conditional
Tag.Alarm = Tag.Temperature > Tag.Setpoint
' Method call
Tag.Efficiency = Script.Class.ServerMain.CalculateEfficiency(Tag.Input, Tag.Output)
' String manipulation
Tag.FullName = Tag.FirstName + " " + Tag.LastName
' Using TIF for conditional execution
Tag.Status = TIF(Tag.Running, "Active", "Stopped")
Step 4: Add External References
Adding DLL References
Go to Scripts → References
- Click Plus icon
- Browse and select DLL file
- Configure:
- Name: Reference identifier
- Domain: Server or Client
- Click OK
DLL Location Best Practices
Location | Macro | Use For |
---|---|---|
ThirdParty folder | _ThirdParty_ | Server-side DLLs |
WpfControls folder | _WpfControls_ | Client-side controls |
Solution folder | _SolutionPath_ | Solution-specific DLLs |
Adding Namespaces
- Open Code Editor
- Click Namespace Declarations button
- Add required namespaces in dialog:
csharp
System.Data
System.Linq
MyCustomLibrary
Never add using/import statements directly in code - always use Namespace Declarations dialog
Step 5: Test and Debug
Enable Debugging
- Go to Runtime → Build and Publish
- Enable Debug Information
- Save solution
Using the Debugger
- Start runtime (F5)
- Go to Runtime → Startup
- Click Connect
- Open script in Code Editor
- Click Attach .NET Debugger
- Set breakpoints by clicking line numbers
- Use step controls when stopped
Monitoring Execution
Check script performance:
csharp
// Access in expressions or displays
@Script.Task.MyTask.ExecutionCount // Number of executions
@Script.Task.MyTask.LastCPUTime // Last execution time
@Script.Task.MyTask.State // Running or Idle
Code Editor Features
IntelliSense
- Type
@
to see all namespaces - Type
.
after objects for properties - Automatic method completion
- Parameter hints
Productivity Tools
Tool | Action | Purpose |
---|---|---|
Format Document | Toolbar button | Auto-format code |
Comment Out | Toolbar button | Comment selection |
Uncomment | Toolbar button | Uncomment selection |
Compile | Save or toolbar | Check for errors |
Toolkit Methods
Common operations via TK
namespace:
csharp
// Type conversion
double value = TK.ConvertTo<double>("123.45");
// Tag to DataTable
DataTable dt = TK.TagsToDataTable("Tag.*");
// Dynamic property access
object val = TK.GetObjectValue("Tag.MyTag");
Common Issues
Script Not Executing
- Check trigger configuration
- Verify InitialState is Enabled
- Review BuildStatus for errors
- Confirm domain (Server/Client) matches deployment
Compilation Errors
- Check BuildStatus column (red X indicates errors)
- Double-click to see error details
- Verify namespace declarations
- Ensure correct code structure (no wrappers)
DLL Not Found
- Use macros instead of absolute paths
- Place in correct folder (ThirdParty/WpfControls)
- Restart Designer after DLL updates
- Check Resolved status in References
Performance Issue
- Monitor LastCPUTime property
- Avoid infinite loops
- Use appropriate trigger type
- Consider execution frequency
Best Practices
Code Structure - Remember: no class/method wrappers in Tasks and Classes
Use descriptive names - Clear task and class identification
Handle exceptions - Platform provides automatic protection, but validate inputs
Reuse code- Create methods in ScriptClasses
Use @ symbol correctly - Required in Tasks/Classes, not in Expressions
Test incrementally - Verify BuildStatus after each change
Monitor performance - Check execution metrics regularly
Use TIF over IIF - Avoid unnecessary method execution in expressions
Script Examples
Data Calculation Task
csharp
// Task body only - no method wrapper
// Trigger: Period = 60000 (1 minute)
double production = @Tag.ProductionCount;
double runtime = @Tag.RuntimeHours;
if (runtime > 0)
{
@Tag.ProductionRate = production / runtime;
@Tag.Efficiency = @Script.Class.ServerMain.
CalculateEfficiency(@Tag.PlannedProduction, production);
}
Alarm Response Expression
vbnet
' Object: Tag.AlarmMessage
' Expression:
TIF(Tag.Critical = 1, "CRITICAL: System Alert", "Normal Operation")
Client Display Initialization
csharp
// ClientStartup Task - body only
@Display.MainPage.UserLabel = @Client.UserName;
@Display.MainPage.LoginTime = DateTime.Now;
@Script.Class.ClientMain.LoadUserPreferences();
Next Steps
- [Display Code Behind →] Scripts in displays
- [Python Integration →] Configure Python scripts
- [API Integration →] External system connections
In this section...