Scripting:Creating plugins
From TSWiki
In this tutorial, I will show you how to create a plugin for a TSW application with scripting support. A plugin - but do they support that? Actually, yes, but perhaps not in the usual way. Scripting is supported, and through that, it's extremely easy to get plugin-like behavior. We will create a .NET assembly to be used by the application, and by doing so, pretty much everything is possible! In this example, I use Visual Studio 2005 to create the plugin, but any modern version of Visual Studio or the free Express versions should be fine, as long as it can make a .NET assembly. In fact, Notepad and a .NET compiler could do the trick as well :)
[edit] Creating the plugin
Now, start Visual Studio and create a new project. The type of the project could actually be Windows Application, since the EXE file produced for an application is an assembly as well, but for creating a plugin, the Class Library option makes more sense. I've called my project "TSWPluginTest", and created it as a C# project, since that's my preferred language. However, VB.NET or any other .NET language will work just as well.
First of all, we wish to create a GUI plugin, so let's add a form to the project. I've called my form "PluginForm". For this simple demonstration, I've created a simple form, which looks like this:
As you can probably guess, this dialog will help you create a new PHP class. It's not terribly useful, but it will demonstrate how easy it is extending a TSW application.
Now, we will need to get some sort of output from this dialog. It could actually be done in several ways - either we could declare the important fields as public and then access them from the scripting code, or we could define a public result method, which we can call from the scripting code, letting the dialog do the work it self. The latter method is probably the cleanest approach, so that's what we'll do.
Here's our simple code to generate the result. As you can see, I've given the controls some more useful names than "textbox1" - you may do the same, or simply change the names in the code snippet.
public string GetResult()
{
StringBuilder sb = new StringBuilder();
if(cbAbstract.Checked)
sb.Append("abstract ");
sb.Append("class " + txtClassName.Text);
if(txtExtends.Text != String.Empty)
sb.Append(" extends " + txtExtends.Text);
sb.Append(" {" + Environment.NewLine + "}");
return sb.ToString();
}
And that's it. Compile the project and then open up the folder where in you created the project. In the bin\debug folder, you will find the created DLL, in my case called TSWPluginTest.dll. Copy this file to your TSW application directory. For WebCoder, this is usually "C:\Program Files\TSW\WebCoder 2007\". Now, start your TSW application and create a new script file from the Scripting menu.
[edit] Creating the script
First of all, you need to add a reference to our new assembly (the DLL file) and then import the PluginForm from it. If you named your project "PluginTest", the output file will be called PluginTest.dll and you will need to add a reference to "PluginTest". Here's my complete scripting example, showing you how easy it is to use our newly created plugin:
clr.AddReferenceByPartialName("TSWPluginTest")
clr.AddReferenceByPartialName("System.Windows.Forms")
from TSWPluginTest import PluginForm
from System.Windows.Forms import DialogResult
form = PluginForm()
if(form.ShowDialog() == DialogResult.OK):
ScriptUtils.InsertCode(form.GetResult())
form.Dispose()
And that's it! Now, go create some cool plugins :)

