This guide 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:
Table of Contents maxLevel 2 minLevel 2 indent 10px exclude Steps style none
ScriptClasses are libraries of reusable methods available throughout your solution.
Class | Purpose | Domain |
---|---|---|
ServerMain | Methods for all server tasks | Server |
ClientMain | Methods for all client displays | Client |
<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
public class ServerMain
{
// 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";
}
ScriptTasks execute code based on triggers or schedules.
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 Type | Example | Use Case |
---|---|---|
Tag Change |
@Tag.Temperature | React to value changes | |
Period | 60000 (ms) | Run every minute |
Condition |
@Tag.Level > 100 | Monitor thresholds |
00:00:30
Daily at 06:00
Startup | Built-in |
One-time initialization |
<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@Tag.ProductionRun (on change)
public void RunTask()
{
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;
}
}
Expressions are single-line VB.NET statements for simple operations.
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 |
Info | ||
---|---|---|
| ||
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. |
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,
@ScriptScript.Class.ServerMain.Method1(),
@ScriptScript.Class.ServerMain.Method2()) ' Only executes Method1() OR Method2(), not both
csharpvbnet
//' Mathematical Tag.Average = (Tag.Value1 + Tag.Value2) / 2
//' Conditional Tag.Alarm = Tag.Temperature > Tag.Setpoint
//' Method call
@ScriptTag.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")
Warning |
---|
Go to Scripts → References
Location | Macro | Use For |
---|---|---|
ThirdParty folder | _ThirdParty_ | Server-side DLLs |
WpfControls folder | _WpfControls_ | Client-side controls |
Solution folder | _SolutionPath_ | Solution-specific DLLs |
csharp
using System.Data;
using MyCustomLibrary;
System.Linq
MyCustomLibrary
Warning |
---|
Never add using/import statements directly in code - always use Namespace Declarations dialog |
Check script performance:
csharp
// InAccess 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
@
to see all namespaces.
after objects for property listpropertiesTool |
---|
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 |
Common operations via TK
namespace:
csharp
// Type conversion
double value = TK.ConvertTo<double>("123.45");
// Tag to DataTable
DataTable dt = TK.CopyTagToDataTableTagsToDataTable(@Tag"Tag.MyTemplate*");
// Dynamic property access
object val = TK.GetPropertyValueGetObjectValue(@Tag"Tag.MyTag, "Value");
Script Not Executing
Compilation Errors
DLL Not Found
Performance
IssuesIssue
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
- Document purpose and parametersUse @ 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
csharp
// Runs every 1 minute
public void CalculateKPIs()
{
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);
}
}
vbnet
//' Object: Tag.
AlarmActiveAlarmMessage
//' Expression: TIF(Tag.Critical =
=1,
@Script.Class.ServerMain.SendAlert(), 0"CRITICAL: System Alert", "Normal Operation")
csharp
// ClientClientStartup StartupTask Task
public- voidbody InitializeClient()
{
only
@Display.MainPage.UserLabel = @Client.UserName;
@Display.MainPage.LoginTime = DateTime.Now;
}@Script.Class.ClientMain.LoadUserPreferences();
Page Tree | ||||
---|---|---|---|---|
|