The following simple VBScript script demonstrates the general approach for writing your own script files. After explaining the most essential aspects of the script, a JScript version and a PerlScript version are presented. Colour-coding is used in a smilar way as in the Script Editor (see next topic): colour blue marks methods provided by BrainVoyager, colour red marks keywords and functions provided by the scripting language and colour purple marks methods provided by the WScript.Shell object (see below).
1: ' LoadDoc.vbs
2:
3: Set BrainVoyager = CreateObject("BrainVoyager.1")
4: Set doc = BrainVoyager.OpenDocument("cg2_tal.vmr")
5: MsgBox("Document loaded !")
6: doc.SetZoomLevel(2)
7: MsgBox("Document zoomed !")
8: doc.Close
9: BrainVoyager.Exit
The line numbers on the left do not belong to the script itself but are used to refer to parts of the script. Line 3 is usually the first executed line in your script since it provides access to BrainVoyager. If BrainVoyager is not running yet, the CreateObject function of VBScript will start BrainVoyager as if you have double-clicked it in Windows Explorer. This works only if BrainVoyager has been registered itself in the registry which normally happens during installation, otherwise CreateObject will report an error. In the latter case, run the "EnableBrainVoyagerScriptServer.bat" file which has been installed in your BrainVoyager folder. If the CreateObject function is successful, it returns a reference to BrainVoyager which is stored in the provided variable, here called "BrainVoyager". If the CreateObject function is executed and there is already an instance of BrainVoyager running, then only a reference to the running BrainVoyager is returned.
Having a reference to a running BrainVoyager now allows to call all methods which are supported by the BrainVoyager Application object. All available methods are described in the "Methods" section of this chapter. One important method of the BrainVoyager application object is OpenDocument which can be used to open any existing BrainVoyager project file. This method is used in line 4 to load the file "C:\cg2_recons\cg2_tal.vmr" or any other file you provide here as input. If the referenced file could be opened successfully, another reference is returned, this time a reference to a BrainVoyager Document object which is stored in the variable "doc". Note that the assignment of both object variables use the VBScript keyword Set. This keyword is not necessary for assignments of standard variables but it must be present when setting object variables. Having a reference to a Document object provides access to many Document methods.
In lines 5 and 7, simple message boxes are shown using the VBScript function MsgBox. The message box is used here to state the successfull execution of script commands. The message box interrupts the execution of the script until the user clicks its OK button.
In lines 6 and 8 two methods of the Document object are called. The SetZoomLevel method is used to change the view of the loaded VMR file. Calling the Close method in line 8 closes the document. If you would not call the Close method (and the Exit method), the script would leave BrainVoyager active with the loaded document. You could then go on working with the loaded file or invoke another script which works with the loaded document.
The final line invokes the Exit method of the Application object which closes BrainVoyager. This command should be the last within a script. As pointed out above, it might be advantageous in some circumstances not to close BrainVoyager, i.e. if a script is used to load a number of files as a starting point for further analysis and visualization steps.
The following script shows how to access BrainVoyager with JScript. In JScript scripts, variables must be declared which is optional in VBScript. The new ActiveXObject statement is the equivalent of the CreateObject function in VBScript. Note that directories within pathes have to be separated in strings with a double "\\" as opposed to a single "\" in VBScript. Since JScript does not have a MsgBox function, the messages for the user are now displayed using the Popup method of the WScript.Shell object. You can, of course, also use this method in VBScript scripts. The Popup method allows to present a message box for a limited amount of time only. The duration has to be specified in seconds, in our example script, a value of 5 seconds is set. Controlling the duration for showing a message box is a nice feature since it is not necessary that a user responds to messages with a button click. This is particular useful in the context of scripts which run demonstrations.
1: // LoadDoc.js
2:
3: var BrainVoyager // Declare variables
4: var doc
5: BrainVoyager = new ActiveXObject("BrainVoyager.1");
6: doc = BrainVoyager.OpenDocument("C:\\cg2_recons\\cg2_tal.vmr");
7: WshShell = new ActiveXObject("WScript.Shell");
8: WshShell.Popup("Document loaded !", 5, "BrainVoyager
Script");
9: doc.SetZoomLevel(2);
10: WshShell.Popup("Document zoomed !", 5, "BrainVoyager
Script");
11: doc.Close();
12: BrainVoyager.Exit();
The final script shows how to access BrainVoyager from PerlScript. To allow automation, the special statement use Win32::OLE must occur prior to access COM servers. The Win32::OLE->new function is the equivalent of the CreateObject function in VBScript. Note, that all (scalar) variables in Perl must begin with a "$" sign.
1: # LoadDoc.pl
2:
3: use Win32::OLE; # use special module to enable automation
4:
5: $BrainVoyager = Win32::OLE->new("BrainVoyager.1");
6: $doc = $BrainVoyager->OpenDocument("C:\\cg2_recons\\cg2_tal.vmr");
7: $WshShell = Win32::OLE->new("WScript.Shell");
8: $WshShell->Popup("Document loaded !", 5, "BrainVoyager
Script");
9: $doc->SetZoomLevel(2);
10: $WshShell->Popup("Document zoomed !", 5, "BrainVoyager
Script");
11: $doc->Close();
12: $BrainVoyager->Exit();