logo WinWrap®

Extend the WinWrap® Basic Scripting Language

WinWrap® Basic is an embedded macro language component available for .NET and COM 32/64 bit Windows applications. The WinWrap® Basic Component is an alternative to Visual Basic for Applications (VBA), ActiveX (e.g. VBScript, JScript, PerlScript, Rexx-based WSH engines and others), and VSTA for this purpose. The WinWrap® Basic Component is compatible with VBA, Sax Basic, VB.NET and Visual Basic 6.0 style scripts.

Create a Scriptable Application

  • Allow users to script your application model's object and events
  • Restrict scripting access to a subset classes, methods, properties and events
  • Promote scriptable class members into the scripting language

Scriptable Architecture

Scriptable Architecture for Extending the WinWrap Basic Scripting Language

Implement Scripting Language Extensions in a Host Application

  • Extend the scripting language with the AddScriptableObjectModel method
  • Mark a "Scriptable" subset of an assembly
  • Interact with the host application via shared IHost interface
  • Access application via language extensions (1, 3)
  • Implement custom behavior via script event handler code (2)

Extend Scripting Language

One or more assemblies can be used to extend the WinWrap® Basic Language. An assembly used to extend the language can be the host's executable assembly or an assembly referenced by the host application. AddScriptableObjectModel extends the WinWrap Basic Scripting Language using the assembly with automatic imports. All scripts are parsed with automatic Imports for each import in the string.

/// Extend WinWrap Basic scripts with Example1.Extensions assembly /// Add "Imports Examples.Extensions" to all WinWrap Basic scripts /// Add "Imports Examples.Extensions.ScriptingLanguage" all WinWrap Basic scripts basicNoUIObj.AddScriptableObjectModel(typeof(ScriptingLanguage));

Mark Classes and Members for Scripting

Use the [Scriptable] attribute on a type or member to enable WinWrap® Basic script access the type or member. Only types and members with the Scriptable attribute are accessible.

[Scriptable] public class Incident { private DateTime Datetime { get; set; } [Scriptable] public event Action Started; [Scriptable] public string FiredBy { private get; set; } [Scriptable] public string FilledInBy { private get; set; } [Scriptable] public string Data { private get; set; } [Scriptable] public void LogMe() { Datetime = DateTime.Now; ScriptingLanguage.Host.Log(ToString()); } /// no [Scriptable] attribute /// allows host to fire the event, but not the script public void Start(string firedby) { FiredBy = firedby; if (Started != null) Started(); } [Scriptable] public override string ToString() { var sb = new StringBuilder(); sb.AppendLine(string.Format(@"Incident at {0:dddd, MMMM yyyy HH:mm:ss tt zzz}", Datetime)); sb.AppendLine(" FiredBy: " + FiredBy); sb.AppendLine(" FilledInBy: " + FilledInBy); if (Data != null) sb.AppendLine(" Data: " + Data); return sb.ToString(); } }

Add the members of a static class to the WinWrap® Basic Scripting Language.

/// AddScriptableObjectModel(typeof(ScriptingLanguage)) /// adds this static class to the scripting language directly [Scriptable] public static class ScriptingLanguage { // accessible by all classes in this assembly internal static IHost Host { get; set; } // make TheIncident available to the script [Scriptable] public static Incident TheIncident { get { return Host.TheIncident; } } // accessible by application, not script public static void SetHost(IHost host) { Host = host; }

Shared IHost Interface

Define a shared interface used by both the host application and the language extensions.

// used by application, not used by WinWrap Basic scripts public interface IHost { Incident TheIncident { get; } void Log(string text); }

Implement the interface in the host application.

public partial class Form1 : Form, IHost { private Incident theIncident_; #region "IHost" public Incident TheIncident { get { return theIncident_; } } public void Log(string text) { textBoxOutput.AppendText(text); }

Scripts use Language Extensions and Implement Custom Application Behavior

'#Language "WWB.NET" ' Subscribe events for object mangaged by host (TheIncident) ' /basic/#/WWB-doc_withevents__def.htm Dim WithEvents anincident1 As Incident = TheIncident Private Sub anincident1_Started() Handles anincident1.Started anincident1.FilledInBy = ScriptName() anincident1.LogMe() End Sub

Conclusion: Scripts are Clear, Concise and Strongly Typed

WinWrap® Basic provides a comprehensive solution to adding scripting to your application. You or your customer can write script code that responds to events fired from objects provided by the application. Scripts are validated for syntactic correctness and all the application has to do is fire the event. This is a great way to script, with the application calling your script code at the appropriate time.

See WinWrap Examples for instructions on how to download and run "Example 1 - NoUI".

Copyright Polar Engineering, Inc.