How to make the application have scripting support
Yangshan River Hu Xiaomin
The background proposed on the market is increasingly large, and the maintenanceability and reusability of software are very important. Everyone wants its own development results to get the largest application, hoping to improve existing systems on the achievements of the original development achievements. Providing secondary development capabilities have become an important way to solve today's growing software development crisis. However, if the API is provided for secondary development, it will bring tricky problems such as language call rules. Based on this, many success systems use Script technology to support secondary development of application systems. Systems that use this model to develop themselves some important, secure features to scripts, allowing scripting interpretation devices to control the use of these features, and implement the reuse of the application software system itself. For example: Action functionality provided in Photoshop can automatically complete the "batch" of the current image.
The traditional script support feature provides a user with an interpreted language. This language is closely related to the system, which can easily achieve the second development needs of the user through the corresponding functional statement. But now the development requires fast and effective, try to control the scale of development, if each application is also committed to the development of a development, and does not say the size of the development, the formation of the language developed will make The headache of the user will only increase the scale and cost of development and the difficulty of maintenance. The development of computer development technology has made us don't have to touch this trouble. To implement scripting support, we can use COM's function, using COM components explained by the existing support for script to implement script support of its own application system. We should remember that in IE provides an independent VBScript interpretation component, we can explain the VBScript and JScript embedded in HTML, and the Office suite also support VBA, which is a classic example of our research COM technology. And in VB6.0, Microsoft provides an ActiveX control Jujube Scripcontrol, which uses it, we can embed the VBScrip interpretation function in the system. If you use C , VB, you can write a language writing application, we can easily implement script support.
The relationship between the Script host, the Script engine, and Script (script) is to implement the COM component-based script support function, we need to correctly understand the Script parties and the relationships between each other. The concepts of scripts are: Script Host, Script Engine (Script Engine), Script.
What is the Script host? Listening to biological terms. Similar to biology concepts, it is easy to think that the function object we have to control is the function we have written, and you want to provide the end user's functional components. For example, Word is a text processing system that Microsoft is provided to everyone. It uses COM-based software structure. If we write some scripts for Word (that is, what is the "macro"), then the object written by the script is Word is Script host. The final executor of the script function is the Script host. Any autonomous application we have written can be seen as a host, which can embed the script interpretation capabilities. Of course, the host should be best based on COM programming. On the other hand, the script is a code for service request or control of the host.
The Script engine is the "bridge" between the Script host and Script. The script engine acquires the script and explains according to the syntax, and control the properties of the object provided by the host program, call the relevant method, and implement the feature of the script requirements. Provide available objects to the script and explain scripts to implement secondary development is the main function of the engine.
The relationship between the three can be represented by the following graphics.
-------------------------------------------------- ----------------------------- Provide objects to script controls
-------------------------------------------------- ------------------------------
Controls the object provided by the host
Get the script to explain from the input part
The properties, methods of the Script engine object, the event Script engine is provided as an ActiveX control to the user, before using it, let's take a look at the properties and methods of this control.
Attributes:
Allowui: Readable, Boolean, and detects whether you are allowed to run user interface elements. If False, the interface element such as a message box is not visible.
CodeObject: Returns a specific public member object called host. Read only.
Modules: Read only. Returns the component library module that supplies to the script. The COM component is usually available to the user to provide a collection of objects that can be left to the user to the user in the form of an object collection, each collecting a modules (module).
Language: Set or get the language explained by the script engine, such as VBScript, JScript.
Name: Returns the module, process, or object's name, read-only properties.
Procedures: Returns the process defined in the module, read only.
SitehWnd: Window handle, if this control is created in ActiveX control, this property is a container of the control; if it is created with an automated object, it is 0, that is, a desktop. This property can be read or written.
State: Set or return to the status of the control, if 0, the control only executes the statement but does not forward the event, 1 for the object to which the object accepted by the added control is forwarded.
Timeout: Set or return the timeout value of the corresponding script of the control. Timeout events will be trigger when the timeout is arrived. It can also be set to -1, indicating that there is no timeout setting.
UsesaFesubset: Set or return to the host program to be safe. The security level of the host program can be set from this property.
Error: Error object, this property returns an error object when an error occurs.
method:
AddCode: Add some code to a module. The format is: AddCode Code, you can call multiple times.
AddObject: Add an object to a module, format: scriptcontrol.addObject (name, object [, addmembers]), name is the object name you want to join, Object is the actual object, and addmembers is optional to indicate whether this object is a global object. False is a partial object. These objects can only be used in the script only after adding objects in this object.
Eval: Value expression. The format is Object.eval (Expression).
ExecuteStatement: Explain and execute a scripting statement. Format: Object.executestatement Statement.
RESET: Discard all objects and code. The State property will be 0.
Run: Run a specified process. Format: Object.run (ProcedureName, Parameters ()), procedurename is the process name to run, Parameters () is
event
There are only two events: Error and Timeout. The former can be used for error capture, which occurs when the script does not get a response.
VB Development Instance Suppose We want to develop a editing software in VB, which is obviously the host in the script technology mentioned above, in order to support the secondary development to design the system based on COM, and provide two object "Word" And "mybox". In fact, in order to simplify the problem, this example does not design a complex code, and the object provided to the user is actually "now" Jujube "Word" is the Word in your system installed in the system, only However, by my "transfer" in "I provide the object" image, the Word all method properties in Office VBA can be provided to the user in the script function of the system; "MyBox" is actually a TextBox added when designing Control, but eventually is provided to the user with my text box. The interface is simple (simple? Speculation?), Try not to transfer everyone's attention to understand the scripting technology described herein. The code and comments are as follows: Version 5.00
'Support this example with an Automation server and an ActiveX control.
Object = "{0e59f1d2-1fbe-11d0-8ff2-00a0d10038bc} # 1.0 # 0"; "msscript.ocx"
Begin Vb.form Form1
CAPTION = "Demonstration System with Script Support
ClientHeight = 4335
ClientLeft = 60
Clienttop = 345
ClientWidth = 4680
Linktopic = "Form1"
Scaleheight = 4335
Scalewidth = 4680
Startupposition = 3 'Windows Default
Begin vb.textbox text2
HEIGHT = 1095
LEFT = 120
MultiLine = -1 'True
TabINDEX = 2
Text = "vbscript.frx": 0000
'Users can enter the code of the property of this object (my object name is mybox) through the script code window, or you can start and control Office Word in your system through the Word object.
Tooltiptext = "Enter code in the script code box to control the properties of this object"
TOP = 3120
Width = 2415
End
Begin Vb.TextBox Text1
HEIGHT = 2295
LEFT = 120
MultiLine = -1 'True
Scrollbars = 3 'Both
TabINDEX = 1
TEXT = "'In this input script code"
Tooltiptext = "You can control the objects provided by two this system in your code: Word and Mybox."
TOP = 480
Width = 4335
End
Begin vb.commandbutton Command1
CAPTION = "Execute Script Code"
BeginProperty Font
Name = "MS SANS Serif"
SIZE = 9.75
Charset = 0
Weight = 400
Underline = 0 'false
Italic = 0 'False
StrikeTHROUGH = 0 'FALSE
EndProperty
HEIGHT = 735
LEFT = 2760 Tabindex = 0
TOP = 3480
Width = 1815
End
'Default attribute of script objects
Begin MsscriptControlctl.ScriptControl ScriptControl1
LEFT = 0
TOP = 2160
_EXTENTX = 1005
_EXTENTY = 1005
Allowui = -1 'True
End
Begin vb.label label1
CAPTION = "Please enter your VB code here"
BeginProperty Font
Name = "MS SANS Serif"
SIZE = 9.75
Charset = 0
Weight = 400
Underline = 0 'false
Italic = 0 'False
StrikeTHROUGH = 0 'FALSE
EndProperty
HEIGHT = 255
LEFT = 120
TabINDEX = 3
TOP = 120
Width = 3255
End
End
Attribute VB_Name = "Form1"
Attribute vb_globalnamespace = false
Attribute vb_creatable = false
Attribute VB_PredecLaredId = True
Attribute VB_EXPOSED = FALSE
Private sub fascist1_click () 'When you click Command1
ScriptControl1.executestatement text1.text 'Explains and executes scripts
End Sub
Private Sub Form_Load () 'Form
DIM OB As Object 'Defines an object variable
Set ob = creteObject ("Word.Application") 'Creates an automation object, reference program ID
ScriptControl1.State = connected 'Set the status of the script control
ScriptControl1.addObject "Word", ob 'Add a received object "word" to the script object
ScriptControl1.addObject "Mybox", Text2 'Add a VB Basic Control Textbox for this Form
End Sub
Examples of operational requirements: VB6.0, PWIN98, and in principle require the Word97 in your system, but if you can modify the createObject ("Word.Application" statement in form_load (), put "word.application" Word97's Progid in the registry replaces the progID of any existing automation objects in your system. After starting this example, you can enter the following code to observe the effect in the script input box, or write your own any code on the property and method of the object provided by the script:
Word.visible = true 'makes the Word object, use it with it through a regular method.
Mybox.text = "This is the content set in the script"
Add a few more than the object provided by the host. You can add any custom objects to the host, in this case, these objects may be VB written objects, VB supplied, including ActiveX controls, automated objects (or even otherwriting, such as Delphi, etc.) Controls, COM objects), but for custom objects must be added to the script control modules with the AddObject method. Control of runtime objects must be confirmed to confirm the validity of the input code when citing objects and properties using Object.xxx. The "effectiveness" refers to the properties, methods, etc. referenced when the script control explains these codes, etc. must be available or valid (some attributes are read only or written, the writing method can not be called in some states), can not be spelling errors . This example does not perform error capture, interested in you can design it yourself.
The script engine in C can use it in C in C in addition to ScriptControl in VB. The use of script features in Visual Studio97 must implement some special interfaces, which is more cumbersome, and this control can save the scriptures in the script support interface. Many of our developed autonomous software do not use script support, resulting in software usage and secondary development, such as WPS97 without Word97 macro function, should be said to be a gap between our domestic software with world software (due to no hand WPS2000, so you can't comment on it). Generally, development software can now use C to easily implement COM. You can easily implement scripting support for your own software, why not do it?
The above throwing jade, hoping to get the advice of colleagues, and I hope to see the relevant domestic software to join the script implementation function, so that the majority of users will participate in the development, after all, "The power of the people is endless"!
-
Written in 1999, is interested in scripting technology