Figure 2   A Simple WSC Component


 <component>
 <registration progid="HelloWorld.WSC"/>
 <public>
     <method name="Welcome"/>
     <property name="Text" />
 </public>
 
 <script language="VBScript">
 Function Welcome
     MsgBox Text
 End Function
 </script>
 </component>

Figure 3   XML Tags in WSC Components

XML Tag
Description
<component>
The root tag that wraps the entire component.
<registration>
Specifies the information that lets you identify the object: progID and/or CLSID, description, version number, and even the routines used to perform registration and unregistration of the component.
<public>
Wraps the automation interface of the component and includes the full list of methods, properties, and events.
<method>
Defines a method by specifying its public name (optionally also its internal name) and the formal parameters it requires. You use the <parameter> tag to specify an argument.
<property>
Defines a property by specifying its public name and optionally also the internal procedures that will provide the reading and writing capability. You do this through the <get> and <put> tags. Omit one of them and you have write-only or read-only properties.
<event>
Defines an event that the component can fire during its lifetime. You must specify a name and optionally one or more arguments using the <parameter> tag.
<implements>
Required if you want your component to provide support for additional, nonautomation interfaces. The range of choices is pretty limited today and includes only ASP and Internet Explorer 5.0 behaviors.
<script>
Any piece of script code used to implement methods, properties, and helper routines. Can accept any scripting language for which there is an engine compatible with the Windows Script ActiveX Scripting engine.
<object>
Defines global objects that can be referenced by the various WSC script blocks. Use it for frequently used objects shared among multiple sections.
<reference>
Includes a reference to an external type library and lets you refer all the mnemonic constants defined. Typically used with the ActiveX Data Objects typelib to import all the predefined constants, such as adUseClient or adStateOpen.
<resource>
Similar to the Visual C++ resource file. Lets you embed literals to be accessed through an ID within the <script> blocks.
<comment>
Any text enclosed by this tag is ignored and is meant to be a comment.


Figure 5   Generating a WSH Type Library


 ' TypeLib.vbs
 ' Generating a type library for the specified WSC file.
 ' The script assumes to be invoked through a context menu 
 ' and to receive the WSC name on the command line.
 ' ---------------------------------------------------------------
 
 ' Get the WSC file name to work with
 If WScript.Arguments.Count = 0 Then
     wscFile = InputBox ("Enter the WSC file name:", "WSC")
 Else
     wscFile = WScript.Arguments.Item(0)
 End If
 if wscFile = "" Then WScript.Quit
 
 
 ' Instantiate the object to create the typelib
 Set oTL = CreateObject("Scriptlet.TypeLib")
 
 
 ' Set source and target file names 
 oTL.AddURL wscFile                
 tlbFile = Replace(wscFile, ".wsc", ".tlb", 1, -1, 1)    
 oTL.Path = tlbFile                  
 
 
 ' Set the name to appear in the Object Browser
 posSlash = InStrRev(wscFile, "\")
 posDot = InStrRev(wscFile, ".")
 wscFileOnly = Mid(wscFile, posSlash+1, posDot-posSlash-1)
 oTL.Name = wscFileOnly & "TLB"
 
 ' Get the description
 defDesc = wscFileOnly & " Type Library"
 desc = InputBox ("Enter the TypeLib description", "WSC Description", defDesc)
 If desc <> "" Then
     oTL.Doc = desc 
 Else
     oTL.Doc = defDesc 
 End If
 
 ' Write the TypeLib
 oTL.Write
 oTL.Reset

Figure 8   A Sample COM Component


 <?xml version="1.0"?>
 <component>
 
 <registration
     description="AspRegExp"
     progid="AspRegExp.WSC"
     version="1.00"
     classid="{396e74e0-59b1-11d3-b17c-00c0dfe39736}"
 >
 </registration>
 
 <public>
     <property name="Mask">
         <get/>
         <put/>
     </property>
     <method name="Validate">
         <parameter name="text" />
     </method>
 </public>
 
 <implements type="ASP" id="ASP"/>
 
 <script language="VBScript">
 <![CDATA[
 
 dim Mask
 
 function get_Mask()
     get_Mask = Mask
 end function
 
 function put_Mask(newValue)
     Mask = newValue
 end function
 
 function Validate(text)
     set re = CreateObject("VBScript.RegExp")
     re.Pattern = Mask
     if re.Test(text) then 
         Response.Write text & " is correct!"
         Validate = true
     else
         Response.Write text & " is incorrect!" 
         Validate = false
     end if
 end function
 
 ]]>
 </script>
 
 </component>

Figure 11   A WSC with Two Scriptlets


 <package>
 
 <component id="one">
 <registration progID="One.WSC" />
 
 <public>
   <method name="Speak" />
 </public>
 
 <script language="VBScript">
 Sub Speak
     MsgBox "Speak"
 End Sub
 </script>
 </component>
 
 <component id=two">
 <registration progID="Two.WSC" />
 
 <public>
   <method name="Tell" />
 </public>
 
 <script language="VBScript">
 Sub Tell
     set wsc = createScriptlet("one")
     wsc.Speak
 End Sub
 </script>
 </component>
 </package>

Figure 12   Script-based COM Component


 <?xml version="1.0"?>
 <component>
 
 <registration
     description="ScriptDtc"
     progid="ScriptDtc.WSC"
     version="1.00"
     classid="{b13312c0-5a0f-11d3-b17c-00c0dfe39736}"
 >
 </registration>
 
 <public>
     <method name="DrawPage" />
     <property name="Title">
         <get/>
         <put/>
     </property>
     <property name="SubTitle">
         <get/>
         <put/>
     </property>
     <property name="Footer">
         <get/>
         <put/>
     </property>
     <property name="Sidebar">
         <get/>
         <put/>
     </property>
     <property name="Content">
         <get/>
         <put/>
     </property>
 </public>
 
 <implements type="ASP" id="ASP"/>
 
 <script language="VBScript">
 <![CDATA[
 
 dim Title
 dim SubTitle
 dim Footer
 dim Sidebar
 dim Content
 
 function get_Title()
     get_Title = Title
 end function
 
 function put_Title(newValue)
     Title = newValue
 end function
 
 function get_SubTitle()
     get_SubTitle = SubTitle
 end function
 
 function put_SubTitle(newValue)
     SubTitle = newValue
 end function
 
 function get_Footer()
     get_Footer = Footer
 end function
 
 function put_Footer(newValue)
     Footer = newValue
 end function
 
 function get_Sidebar()
     get_Sidebar = Sidebar
 end function
 
 function put_Sidebar(newValue)
     Sidebar = newValue
 end function
 
 function get_Content()
     get_Content = Content
 end function
 
 function put_Content(newValue)
     Content = newValue
 end function
 
 sub DrawPage()
     response.write "<table border=0 width=100% bgcolor=blue>"
     response.write "<tr><td class=Title>"
     response.write Title 
     response.write "</td></tr>"
     response.write "<tr><td class=SubTitle>"
     response.write SubTitle 
     response.write "</td></tr></table>"
 
     response.write "<table border=0 width=100% height=85%><tr>"    
     response.write "<td class=Sidebar>"    
     response.write Sidebar
     response.write "</td>"
     response.write "<td class=Content>"    
     response.write Content
     response.write "</td>"
     response.write "</tr></table>"
 
     response.write "<table border=0 height=5% width=100%>"
     response.write "<tr><td class=Footer>"
     response.write Footer 
     response.write "</td></tr></table>"
 end sub
 
 ]]>
 </script>
 
 </component>

Figure 14   Defining a Behavior


 <?xml version="1.0"?>
 <component>
 
 <registration
     description="TextBox"
     progid="TextBox.WSC"
     version="1.00"
     classid="{035dba00-5a1a-11d3-b17c-00c0dfe39736}"
 >
 </registration>
 
 <public>
     <property name="Mask">
         <get/>
         <put/>
     </property>
     <property name="Tip" />
     <property name="TipControl" />
     <event name="ValidationFailed"/>
 </public>
 
 <implements type="Behavior" id="Behavior">
     <attach event="onblur" handler="event_Validate" />
 </implements>
 
 <script>
 <![CDATA[
 
 var Mask;
 var bgColor;
 
 bgColor = element.style.background;
 
 function event_Validate() {
     re = new ActiveXObject("VBScript.RegExp");
     re.Pattern = Mask;
     if (re.Test(value)) {
         element.style.background = bgColor;
         HideTip();
     } 
     else {
         ShowTip();
         element.style.background = "hotpink";
     }
 } 
 
 function get_Mask() {
     get_Mask = Mask;
 }
 
 function put_Mask(newValue) {
     Mask = newValue;
 }
 
 function ShowTip() {
     elem = document.all(TipControl); 
     elem.innerHTML = Tip;
     elem.style.fontFamily = "verdana";
     elem.style.fontSize = "12";
     elem.style.padding = 4;
     elem.style.display = "";
 }
 
 function HideTip() {
     elem = document.all(TipControl); 
     elem.style.display = "none";
 }
 ]]>
 </script>
 
 </component>