WSCs - A Quick How-To:
This is a quick demonstration of how to use a windows script component, to create an external library with script functions and objects, that can be re-used within DO by any button or script-addin. Actually it can be used by any software development environment on windows (wsh, perl, asp, python, php, c#, c++, etc.).
Introduction:
A windows script component basically acts like a regular DLL, but it's functionality can be pure script code (jscript or vbscript by default).
This demo makes use of jscript to create a DOpus helper object, which offers the following two methods.
GetStringLength(s) - returns the length of the passed string
Sum(a,b) - sums and returns two variables
It also has a property "MyProperty" which will be fetched and set and then refetched to, well just to show-case that it works! o)
Installation:
Download the "DOpusHelper.wsc.txt" file, remove the "*.txt" extension and put it anywhere you like. I suggest "/dopusdata/scripts". Then run "regsvr32.exe x:\path\to\DOpusHelper.wsc" in a command prompt to register the script component once! You don't need to repeat this step, whenever you change or extend the script component!
Now copy/paste or download the button below and put it to work on a DOpus toolbar. Make sure to toggle the "Other Logs" utility pane visible to see it's output.
Usage:
Hit the button and see the DOpus log output. You should see something like this:
Windows Script Component - Demo:
WSC-Function 'GetStringLength()' returned: 12
WSC-Function 'Sum()' returned: 3
WSC-Property (get #1): MyValue
WSC-Property (get #2): I changed it from DO!
Resources:
This is the button that "loads" the external helper (the windows script component) and uses it's functionality.
function OnClick( data ){
DOpus.Output("Windows Script Component - Demo:");
//creating an instance of our scripted helper
var helper = new ActiveXObject("DOpusHelper.wsc");
//calling GetStringLength() from helper
var length = helper.GetStringLength( 'hello world!');
DOpus.Output("WSC-Function 'GetStringLength()' returned: " + length);
//calling Sum() from helper
var a = 1;
var b = 2;
DOpus.Output("WSC-Function 'Sum()' returned: " + helper.Sum( a, b));
//getting/setting a property of the helper
DOpus.Output("WSC-Property (get #1): " + helper.MyProperty);
helper.MyProperty = "I changed it from DO!";
DOpus.Output("WSC-Property (get #2): " + helper.MyProperty);
}
WSC-Demo_DOpusHelper.dcf (1.78 KB)
This is the content of "DOpusHelper.wsc", as you can see, it is basically XML with injected jscript code. Any decent windows text editor should be able to display this file with proper syntax highlighting, even though xml and jscript are mixed up (Editplus can).
<?xml version="1.0"?>
<component>
<?component error="true" debug="true"?>
<registration
description="DOpusHelper.wsc"
progid="DOpusHelper.wsc"
version="1.00"
classid="{92012337-9912-4abb-a612-8f3226dc126a}">
</registration>
<public>
<!-- ////////////////////////////////////////////////////////////////// -->
<property name="MyProperty" get="get_MyProperty" put="put_MyProperty"></property>
<!-- ////////////////////////////////////////////////////////////////// -->
<method name="GetStringLength">
<parameter name="Param1"/>
</method>
<!-- ////////////////////////////////////////////////////////////////// -->
<method name="Sum">
<parameter name="Param1"/>
<parameter name="Param2"/>
</method>
</public>
<script language="JScript">
<![CDATA[
var MyProperty = "MyValue";
///////////////////////////////////////////////////////////////////////////////
// properties setting/getting
///////////////////////////////////////////////////////////////////////////////
function get_MyProperty(){
return MyProperty;
}
///////////////////////////////////////////////////////////////////////////////
function put_MyProperty( newValue ){
MyProperty = newValue;
}
///////////////////////////////////////////////////////////////////////////////
// methods
///////////////////////////////////////////////////////////////////////////////
function GetStringLength( param1){
return String(param1).length;
}
///////////////////////////////////////////////////////////////////////////////
function Sum( param1, param2){
return (param1 + param2);
}
///////////////////////////////////////////////////////////////////////////////
]]>
</script>
</component>
DOpusHelper.wsc.txt (2.12 KB)
Hint:
Whenever you want to create a new script component, you need to create a new *.wsc file and, very important, also change the internal classid of that new script component file. A much better way to create new script components is the windows script component wizard offered by microsoft, it takes care about the ids and puts in all necessary information automatically.