Events Print this Topic Previous Page Up One Level Next page

Home >  Programmers' Reference > Scripting Editor >


The Events folder of the scripting project (see screenshot below) contains folders for the following type of events:


Application Events
Document Events
Authentic View Events
Grid View Events
Text View Events


Note that these events are XMLSpy-specific, as opposed to Form-based events. Each of the folders listed above contains a set of events for which Event Handler scripts can be written.


Application Events, for example, are shown in the screenshot below.


To access the event handler script of any of these events, right-click the event and select Open from the context menu. The script will be displayed in the Main Window (see screenshot below) and can be edited there. After you have finished editing the script, save changes by clicking the Save command in the toolbar of the Scripting Editor.




Note the following points:


Event Handlers need function headers with the correct spelling of the event name. Otherwise the Event Handler will not be called.
It is possible to define local variables and helper functions within Macros and Event Handlers. Example:


//return value: true allows editing

//return value: false disallows editing

var txtLocal;

function Helper()


 txtMessage = txtLocal;



function On_BeforeStartEditing(objXMLData)


 txtLocal = "On_BeforeStartEditing()";




In order for events to be processed, the Process Events options must be toggled on in the Scriptings options of XMLSpy. See Scripting Projects in XMLSpy for details.
Also see Programming Points.



Application Events



The OnInitialize event is raised after the main window becomes visible but before any project is loaded. This event is not raised if the application can't be loaded at all.



If the application is completely loaded and after the OnInitialize event occurs, the OnRunning event is raised.



The event is raised after any open project and all documents have been closed on shutdown of the application. The main window is no longer visible.



The following script is an Event Handler for the On_BeforeOpenProject event. It allows you to add a script that will be executed each time before XMLSpy opens a project. The example script below sequentially opens all XML files located in the XML folder of the project and validates them. If the validation fails, the script shows the validation error and stops. If a file passes the validity test, it will be closed and the next file will be opened.


Enter the following script for the On_BeforeOpenProject() event, and then save the scripting project.


function On_BeforeOpenProject()


         var        bOK;

         var        nIndex,nCount;

         var        objItems,objXMLFolder = null;


         objItems = Application.CurrentProject.RootItems;

         nCount = objItems.Count;


         // search for XML folder

         for(nIndex = 1;nIndex <= nCount;nIndex++)        {

                 var txtExtensions;

                 txtExtensions = objItems.Item(nIndex).FileExtensions;


         if(txtExtensions.indexOf("xml") >= 0)        {

                 objXMLFolder = objItems.Item(nIndex);





 // does XML folder exist?

 if(objXMLFolder)        {

         var objChild,objDoc;


         nCount = objXMLFolder.ChildItems.Count;


         // step through associated xml files

         for(nIndex = 1;nIndex <= nCount;nIndex++)        {

                 objChild = objXMLFolder.ChildItems.Item(nIndex);


                 try        {

                         objDoc = objChild.Open();


                         // use JScript method to access out-parameters

                         var strError = new Array(1);

                         var nErrorPos = new Array(1);

                         var objBadData = new Array(1);


                         bOK = objDoc.IsValid(strError,nErrorPos,objBadData);


                         if(!bOK)        {

                                 // if the validation fails, we should display the

                                 // message from XMLSpy

                                 // of course we have to create the form "MsgBox" and

                                 // define the global txtMessage variable


                                 // txtMessage = Position:" + nErrorPos[0] + "\n" + // strError[0];

                                 // txtMessage +=        "\n\nXML:\n" + objBadData[0].Name + ", " +

                                 //                        objBadData[0].TextValue;


                                 // Application.ShowForm("MsgBox");






                         objDoc = null;


                 catch(Err)        {

                         // displaying the error description here is a good idea


                         // txtMessage = Err.Description;        

                         // Application.ShowForm("MsgBox");









Testing the Event Handler

Switch to XMLSpy, and open a project to see how the BeforeOpenProject event is handled.


© 2019 Altova GmbH