VB Script Reference
VB Script Reference
Summary
This reference manual describes the VB Script language
Technical Reference used in Altium Designer.
TR0125 (v1.4) Sept 1, 2006
For detailed information on VB Script and its keywords, operators and statements, please refer to
Microsoft Developers Network website, http://msdn.microsoft.com/library/en-
us/script56/html/vtoriVB Script.asp.
Altium Designer and Borland Delphi Run Time Libraries
The Scripting system also supports a subset of Borland Delphi Run Time Library (RTL) and a subset of
Altium Designer RTL which is covered in the RTL Reference in the Scripting Online Help in Altium
Designer.
There are several Object Models in Altium Designer; for example you can use the PCB Object Model in
your VB Scripts to deal with PCB objects on a PCB document, WorkSpaceManager Object Model to
work with Projects and their documents and extract netlist data for example.
The Scripting Online Reference Help contains information on interfaces with respect to Altium Designer
Object Models, components, global routines, types, and variables that make up this scripting language.
You can consult the Visual Basic documentation by Microsoft for more information on VB Script
functions.
Server Processes
A script can execute server processes and thus server processes and parameters are covered in the
Server Process Reference.
Example of a Subroutine
Sub Log(Description, Data)
Call ReportFile.Write(Description)
Call ReportFile.WriteLine(Data)
End Sub
Example of a subroutine less script.
' script here with no function/sub routine
A = 50
A = A + 1
ShowMessage(IntToStr(A))
ShowMessage(IntToStr(A))
Now, only parameter-less functions and procedures for each script of an opened project only appear
on the Select Item to Run dialog. It is a good idea for script writers to write the functions in scripts so
that they will appear in this dialog and the other functions with parameters not to appear in this same
dialog.
When you are working in a different editor such as PCB editor, you can assign the script to a process
launcher and use it to run a specified script easily. See the Assigning a script to a process launcher.
You can add a list of installed script projects so that, every time you invoke the Select item to Run
dialog, the installed script projects will appear along with other script projects currently open in the
Projects panel. Invoke Scripting System Settings item from Tools » Editor Preferences menu in
the TextEditor workspace and then drill down to the Altium Designer System, Scripting System, and the
Scripting System page appears.
2. Using the Run Process dialog to execute a script
Invoke the Run Process dialog from Altium Designer's System menu and execute the
ScriptingSystem:RunScript process in the Process: field and specify the script parameters, the
ProjectName parameter which is the path to the project name and the ProcName parameter to
execute the specified procedure from a specified script in the Parameters: field.
You need the following parameters for the ScriptingSystem:RunScript process to execute a specified
script.
Process:
ScriptingSystem:RunScript
Parameters:
ProjectName (string)
ProcName (string)
Example
Process: ScriptingSystem:RunScript
Parameters : ProjectName = C:\Program Files\Altium Designer 6\Examples\Scripts\VB
Scripts\HelloWorld.PrjScr | ProcName = HelloWorld>HelloWorld.
To run a script repeatedly in the text editor in Altium Designer, assign the script to the Set Project
Startup Procedure item from the Run menu of the Text editor server. you can then click on the Run
button. or press F5 to execute this script. To run a different script, you will need to re-invoke the Set
Project Startup Procedure from the Run menu and assign a new script to it.
You can click on the Run Script item from the Altium Designer system menu and the Select Item to
Run dialog appears with a list of procedures (those parameterless procedures/functions only appear)
within each script in a project. This may be needed if you wish to run a script on a specific document
type such as PCB or Schematic documents.
You can also use the Run Process dialog and specify the scriptingsystem server process and specify
the parameters for this scripting system server to execute a script, this may also be needed if you wish
to run a script on a specific document type such as PCB or Schematic documents.
5. You will need to give a name to this new command and assign a new icon if you wish. In this case,
the name is PCBScript in the Caption: field of this dialog. The new commands appear in the
[Custom] category of the Categories list. Click on the [Custom] entry from the Categories list.
The PCBScript command appears in the Commands list of this dialog.
6. You then need to drag the new PCBScript command onto the PCB menu from the Customizing
PCB Editor dialog. The command appears on the menu. You can then click on this new command
and the HelloWorld dialog appears.
SubRoutine parameters
Subroutine example
Sub SetTheHeight AHeight
Set Component.Height = AHeight
End Sub
Function example
Function Addone(value)
AddOne = Value + 1
End Function
A longer example
Function Test(s)
Test = S + " rules.."
End Function
Sub Main
Dim S
S = "Altium Designer"
DisplayName Test(s)
End Sub
Parameters and Arguments
The procedure declaration normally has a list of parameters (remember variables are considered
typeless and the scripting system works out automatically what the variable types are). The value used
in place of the parameter when you make a procedure call is called an argument.
Example of a subroutine with a parameter.
Sub DisplayName (sName)
MsgBox "My Name is " & sName
End Sub
Example of calling a subroutine
Sub Main
DisplayName "Altium Designer Rules"
End Sub
Notes
8 TR0125 (v1.4) Sept 1, 2006
VB Script Reference
The use of the Call keyword to invoke a subroutine or a function is optional (maintained for backward
compatibility).
Example
' Checks if the current document is a Schematic document
If SchServer Is Nothing Then Exit Sub
Set CurrentSheet = SchServer.GetCurrentSchDocument
If CurrentSheet Is Nothing Then Exit Sub
To have access to a PCB document, you invoke the PCBServer.
Creation of a PCB object using the PCB Object model
Sub ViaCreation
Dim Board
Dim Via
Objects, Interfaces, functions and types in your scripts can be used from the following:
• Client API
• PCB Server API
• Schematic Server API
• Work Space Manager Server API
• Nexus API
• Altium Designer RTL functions
• Parametric processes.
VB Script Keywords
The scripting system supports the VB Script language which is derived from the Microsoft Active
Scripting language technology.
VB Script Statements
In this section:
• Conditional statements
• Expressions and Operators
Conditional statements
The main conditional statements supported by the VB Script;
• If Then
• For Next Loop
• Exit For
• For Each Next
• Do Loop
• While WEnd
• Select Case
You have to be careful to code your scripts to avoid infinite loops, ie the conditions will eventually be
met.
The Do Loop
The Do Loop has several loop variations.
1.
Do while until condition
' code block
Loop
2.
Do
' code block
Loop while until condition
3.
Do
' code block
Loop
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiation
Mod Modulo
String Operators
& Concatenation
Logical Operators
Or Logical OR
XOR
Eqv
Imp
&
The difference between the two methods is that ByRef passes a reference to the variable passed and
allows the sub routine or function to make changes to the actual variables that are passed in as
parameters (this is the default method of passing parameters and is used if the method is not explicitly
declared).
The ByVal passes the value of the variable only. The sub routine or function can use this value, but the
original variable passed is not altered.
The following examples illustrate the differences between methods. The main procedure is as follows:
Sub Main
Dim X, Y
X = 45 : Y = "Number"
Test X, Y ' Call to a subprocedure called Test.
MsgBox X
MsgBox Y
End Sub
The above procedure includes a call to a subprocedure, Test. If the subroutine is defined as follows:
then the variables X and Y in the main procedure are referenced directly by the sub routine. The result
is that the values of X and Y are altered by the sub routine so that after the Test is executed X = 450
and Y = "Number = 45".
If, however, the sub routine is defined as follows:
Then after Test is executed X = 45 and Y = "Number", i.e. they remain unchanged.
If the sub routine is defined as follows:
Then after Test is executed, X = 450 and Y = "Number". Because Y was passed by value, it remains
unchanged.
You can override the ByRef setting of a function or sub routine by putting parentheses around a
variable name in the calling statement. Calling Test with the following statement:
Test (X), Y
would pass the variable X by value, regardless of the method defined for that parameter in the
procedure definition.
File IO Routines
The VB Script language set supports a set of File IO routines:
• Dir
• FileLen
• FileTimeDate
• FileCopy
• Kill
• Name
• RmDir
• MkDir
Math Routines
The VB Script language set supports a set of Math routines:
• Abs
• Atn
• Cos
• Exp
• Log
• Not
• Oct
• Rnd
• Sin
• Sqn
• Tan
String Routines
The VB Script language set supports a set of String routines and a few string routines outlined below:
• Asc
• Chr
• Format
• InStr
• InStrRev
• LCase
• Len
• Left
• Mid
• Right
• Str
• Trim
• LTrim
• RTrim
• UCase
• CheckActiveServer
• GetActiveServerName
• GetCurrentDocumentFileName
• RunApplication
• SaveCurrentDocument
Useful Dialogs
• ConfirmNoYes
• ConfirmNoYesCancel
• ShowError
• ShowInfo
• ShowWarning
Components
The scripting system handles two types of components: Visual and Nonvisual components. The visual
components are the ones you use to build the user interface, and the nonvisual components are used
for different tasks such as these Timer, OpenDialog and MainMenu components. You use the Timer
nonvisual component to activate specific code at scheduled intervals and it is never seen by the user.
The Button, Edit and Memo components are visual components for example.
Both types of components appear at design time, but non visual components are not visible at runtime.
Basically components from the Tool Palette panel are object orientated and all these components
have the three following items:
• Properties
• Events
• Methods
A property is a characteristic of an object that influence either the visible behaviour or the operations of
this object. For example the Visible property determines whether this objet can be seen or not on a
script form.
An event is an action or occurrence detected by the script. In a script the programmer writes code for
each event handler which is designed to capture a specific event such as a mouse click.
A method is a procedure that is always associated with an object and define the behavior of an object.
All script forms have one or more components. Components usually display information or allow the
user to perform an action. For example a Label is used to display static text, an Edit box is used to
allow user to input some data, a Button can be used to initiate actions.
Any combination of components can be placed on a form, and while your script is running a user can
interact with any component on a form, it is your task, as a programmer, to decide what happens when
a user clicks a button or changes a text in an Edit box.
The Scripting system supplies a number of components for you to create complex user interfaces for
your scripts. You can find all the components you can place on a form from the Toolbox palette.
To place a component on a form, locate its icon on the Tool Palette panel and double-click it. This
action places a component on the active form. Visual representation of most components is set with
their set of properties. When you first place a component on a form, it is placed in a default position,
with default width and height however you can resize or re-position this component. You can also
change the size and position later, by using the Object Inspector.
When you drop a component onto a form, the Scripting system automatically generates code
necessary to use the component and updates the script form. You only need to set properties, put code
in event handlers and use methods as necessary to get the component on the form working.
sub bCancelButtonClick(Sender)
ModalResult := mrCancel
end sub
sub RunShowModalExample
'Form Visible property must be false for ShowModal to work properly.
If Form.ShowModal = mrOk Then ShowMessage("mrOk")
If Form.ShowModal = mrCancel Then ShowMessage("mrCancel")
end sub
The ModalResult property example here is a bit more complex. The following methods are used for
buttons in a script form. The methods cause the dialog to terminate when the user clicks either the OK
or Cancel button, returning mrOk or mrCancel from the ShowModal method respectively.
You could also set the ModalResult value to mrOk for the OK button and mrCancel for the Cancel
button in their event handlers to accomplish the same thing. When the user clicks either button, the
dialog box closes. There is no need to call the Close method, because when you set the ModalResult
method, the script engine closes the script form for you automatically.
Note, if you wish to set the form's ModalResult to cancel, when user presses the Escape key, simply
enable the Cancel property to True for the Cancel button in the Object Inspector panel or insert
Sender.Cancel := True in the form's button cancel click event handler.
Accepting input from the user
One of the common components that can accept input form the user is the EditBox component. This
EditBox component has a field where the user can type in a string of characters. There are other
components such as masked edit component which is an edit component with an input mask stored in
a string. This controls or filters the input.
The example below illustrates what is happening, when user clicks on the button after typing something
in the edit box. That is, if the user did not type anything in the edit component, the event handler
responds with a warning message.
sub TScriptForm.ButtonClick(Sender)
If Edit1.Text = “” Then
ShowMessage(“Warning - empty input!”)
Exit
End
' do something else for the input
End sub
Note, A user can move the input focus by using the Tab key or by clicking with the house on another
control on the form.
Responding to events
When you press the mouse button on a form or a component, Altium Designer sends a message and
the Scripting System responds by receiving an event notification and calling the appropriate event
handler method.
form, Altium Designer sends a message to the script and the script reacts to this new event. If the
OnClick event for a button is specified it gets executed.
The code to respond to events is contained in event handlers. All components have a set of events that
they can react on. For example, all clickable components have an OnClick event that gets fired if a
user clicks a component with a mouse. All such components have an event for getting and loosing the
focus, too. However if you do not specify the code for OnEnter and OnExit (OnEnter - the control has
focus; OnExit - the control loses focus) the event will be ignored by your script.
Your script may need to respond to events that might occur to a component at run time. An event is a
link between an occurrence in Altium Designer such as clicking a button, and a piece of code that
responds to that occurrence. The responding code is an event handler. This code modifies property
values and calls methods.
List of properties for a component
To see a list of properties for a component, select a component and in the Object Inspector, activate
the Properties tab.
List of events for a component
To see a list of events a component can react on, select a component, and in the Object Inspector
activate the Events tab. To create an event handling procedure, decide on what event you want your
component to react, and double click the event name.
For example, select the Button1 component from the Toolbox panel and drop it on the script form, and
double click next to the OnClick event name. The scripting system will bring the Code Editor to the top
of the Altium Designer and the skeleton code for the OnClick event will be created.
For example, a button has a Close method in the CloseClick event handler. When the button is clicked,
the button event handler captures the on click event, and the code inside the event handler gets
executed. That is, the Close method closes the script form.
In a nutshell, you just select a button component, either on the form or by using the Object Inspector
panel, select the Events page, and double click on the right side of the OnClick event, a new event
handler will appear on the script. OR double click on the button and the scripting system will add a
handler for this OnClick event. Other types of components will have completely different default
actions.
List of methods for a component
To see a list of methods for a component, see the Components Reference.
Index
A M
About VB Script examples ....................................... 6 Math Routines for VB .............................................18
Adding scripts to a project ........................................ 3 P
Assigning a VB script to a process launcher............ 5 Passing parameters to procedures.........................16
C R
Components for VB Scripts .................................... 21 Reserved words in VB Script..................................11
Conditional statements........................................... 12 S
Creating new VB scripts ........................................... 2 Server Process Routines........................................19
D Splitting a line of VB script........................................9
Dates Times for VB ................................................ 18 String Routines for VB ............................................19
Designing Script Forms with VB Script .................. 22 U
E Using components in your scripts...........................24
Executing a script in DXP......................................... 3 Using DXP Objects in VB scripts..............................9
Exploring the VB Script Language ........................... 1 V
Expressions and Operators in VB Script................ 14 VB Script Functions ................................................16
F VB Script Keywords................................................11
File IO Routines...................................................... 18 VB Script naming conventions..................................7
Forms and Components with VB Script ................. 21 VB Script source files ...............................................2
I VB Script Statements .............................................12
Including comments in VB scripts ............................ 9 VB Subroutines and Functions .................................7
L W
Local and Global variables in VB ............................. 7 Writing Event Handlers...........................................23
Writing VB Script scripts ...........................................7
Revision History
6-Jul-2006 1.3 Updated for Altium Designer 6.3 DXP references changed to Altium
Designer. For To Next syntax corrected and While WEnd loop added.
1-Sept-2006 1.4 Updated for Altium Designer 6.4 Typing of variables corrected. Variables
are typeless and functions declaration clarified.