10 Powerful TMS Scripter Scripts Every Developer Should Know
TMS Scripter brings scripting to Delphi/C++Builder applications, letting you embed, execute, and manage scripts at runtime. Below are ten powerful scripts—each with purpose, when to use it, and a compact implementation or pseudocode you can adapt. Examples assume TMS Scripter with PascalScript or JavaScript engines where noted.
1. Runtime Configuration Loader
- Purpose: Load app settings from an external file and apply them without recompiling.
- When to use: Enable end-user configuration or quick toggles during debugging.
- Sketch (PascalScript):
pascal
procedure LoadConfig(fname: string); var cfg: TStringList; begin cfg := TStringList.Create; try cfg.LoadFromFile(fname); // parse simple key=value lines for i := 0 to cfg.Count-1 do ApplySetting(ParseKey(cfg[i]), ParseValue(cfg[i])); finally cfg.Free; end; end;
2. Dynamic UI Modifier
- Purpose: Modify forms, controls, and layouts at runtime via script.
- When to use: A/B testing UI variants, quick fixes, or exposing a plugin system.
- Sketch (PascalScript):
pascal
procedure SetControlVisible(formName, ctrlName: string; show: Boolean); var f: TForm; c: TControl; begin f := FindFormByName(formName); if Assigned(f) then begin c := f.FindComponent(ctrlName) as TControl; if Assigned(c) then c.Visible := show; end; end;
3. Hotfix Injector
- Purpose: Patch small logic bugs at runtime without redeploying.
- When to use: Critical bug fixes while preparing a formal release.
- Sketch:
Provide a script hook that replaces or wraps existing method calls. Example pattern: register a script callback for an event, check conditions, and return alternate results.
4. Data Migration Utility
- Purpose: Run one-off or repeatable migrations on local databases or files.
- When to use: Upgrading user data formats between releases.
- Sketch (PascalScript):
pascal
procedure MigrateUserData; begin // open DB, iterate records, transform fields, save DB.Open(‘users.db’); while not DB.EOF do begin DB.FieldByName(‘fullname’).AsString := TransformName(DB.FieldByName(‘fullname’).AsString); DB.Post; DB.Next; end; DB.Close; end;
5. Automated Testing Hook
- Purpose: Drive UI flows for regression tests or demo scripts.
- When to use: Create reproducible sequences for QA or demos.
- Sketch:
pascal
procedure RunLoginTest; begin SetText(‘LoginForm’,‘edtUser’,‘test’); SetText(‘LoginForm’,‘edtPass’,‘password’); Click(‘LoginForm’,‘btnLogin’); AssertVisible(‘MainForm’,‘lblWelcome’); end;
6. Metrics & Telemetry Sender
- Purpose: Collect runtime metrics and send anonymized telemetry (comply with privacy rules).
- When to use: Monitor feature usage or performance in production.
- Sketch:
Script collects counters and sends them via HTTP POST to a configured endpoint, batching to reduce overhead.
7. Scripting Console / REPL
- Purpose: Provide an in-app console for admins or power users to run ad-hoc commands.
- When to use: Diagnostics, exploration, or advanced user scenarios.
- Sketch:
Expose application objects to the script engine and implement an input/output pane that evaluates statements and prints results or exceptions.
8. Plugin Loader
- Purpose: Discover and run scripts as plugins stored in a directory.
- When to use: Let third-parties extend app via scripts.
- Sketch:
pascal
procedure LoadPlugins(dir: string); var files: TStringList; i: Integer; begin files := FindFiles(dir,’*.pas’); for i := 0 to files.Count-1 do Scripter.LoadScriptFromFile(files[i]); end;
9. Scheduled Task Runner
- Purpose: Run scheduled background tasks (cleanup, backups, sync).
- When to use: Periodic maintenance or offline processing.
- Sketch:
Use a timer in the host app to invoke named scripts at intervals; scripts implement the task logic (e.g., PurgeOldRows).
10. Security Gatekeeper (Scripted Policy Checks)
- Purpose: Enforce dynamic access rules or feature flags via scripts.
- When to use: Complex or frequently changing authorization rules.
- Sketch:
Expose user/context info to scripts; script returns allow/deny. Host evaluates result before granting actions.
Best Practices
- Expose only a limited, well-documented API to scripts—avoid exposing raw pointers or file system access unless necessary.
- Sandbox scripts (timeout, memory limits) and validate inputs.
- Sign or checksum production scripts if allowing third-party plugins.
- Log script errors with stack traces and safe context for debugging.
- Keep scripts idempotent for migrations and scheduled tasks.
Example: Simple PascalScript that toggles dark mode
pascal
procedure ToggleDarkMode(enable: Boolean); begin if enable then ApplyTheme(‘Dark’) else ApplyTheme(‘Light’); SaveConfig(‘theme’,‘dark’, enable); end;
Use these scripts as templates—adapt types, object names, and engine specifics (PascalScript vs JavaScript) to your project.
Leave a Reply