Segments: Script Includes

Introduction to Script Includes
Beginning with the DesktopX 3.2 Betas, DX has the ability to "make its scripts external" using Object.SetScript. This capability has some drawbacks, namely, only one script can be set and that script is a total replacement of the traditional DesktopX script.

I found a more flexible solution in this Cutting Edge article (from November 2000). By adding a short function, Include(vbsFilePath), to the body of our DX script, we can "include", or incorporate the contents of, any number of external vbs files. This is achieved by Call Include("MySampleFileName.vbs") statements in the Object_OnScriptEnter Sub, as shown below.

Demonstration Code
Option Explicit

'Called when the script is executed Sub Object_OnScriptEnter Call Include("C:/MyScripts/ButtonBehaviorLibrary.vbs") Call Include("C:/MyScripts/HandyStringManips.vbs") End Sub

' /// ' Any Object specific code you want can be written ' and saved here in the DX script ' ///

' /// ' The Included files will behave as if ' their text is in this location ' directly above the Include function. ' This rarely matters, far as I can tell ' ///

Function Include(vbsFile) Dim fso, f, s  Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile(vbsFile) s = f.ReadAll f.Close ExecuteGlobal s  Set fso = Nothing Set f = Nothing End Function

'Called when the script is terminated Sub Object_OnScriptExit

End Sub

As you can see, this can be a helpful tool. If you add scripts as Custom Files to an Object from the Summary panel, they can be referenced: Call Include(Object.Directory & "MyFile.vbs") in the script. This is helpful for packaging, but to update such an included file you need to either re-add the file to Custom Files or overwrite directly in the Object.Directory (access yours through DXBuilder main panel). Whether the file is a local like this or absolute reference, the dx script must be restarted to show changes made in the included files (as with Object.SetScript).

The Include(vbsFile) function itself does not exactly have to be understood for this to be useful. The FileSystemObject is useful enough that it should be familiar to all DX-Scripters, but the ExecuteGlobal is a bit more esoteric.

Comments/Suggestions
None right now. :-)