DesktopX: Scripting - The 4 Basics of Vbscript

From WinCustomize Wiki

Jump to: navigation, search


The 4 Basics of Vbscript
Original Author:sViz
Date Created:August 11, 2007
Application:DesktopX
Programs Used:DesktopX


Okay, so as previously demonstrated it’s pretty easy to get started in DesktopX and there’s a wide range of things you can make. But, truth be told, the real power of DX lies in coding. Don’t panic, we’re not about to go warp speed and enter the 12th dimension or anything. Basic coding is not as difficult as it sounds. It’s all about creating simple procedures that make your widget do some powerful things.

Now I won’t lie, getting really good at coding takes hard work & determination, and some foreknowledge of vbscript would be good here. However, a little help goes a long way, and that’s what we’re here for! This tutorial series will cover some basic tasks/commands/functions that you’ll likely be using a lot in future when scripting your widgets. Hopefully, it will help you get into the joy DXscripting and I'll be seeing your widgets in the near future.


Contents

The 4 Basics of Vbscript

Vbscript is the language of coding that is most commonly used in DesktopX. In this part of the tutorial series I’m going to give an introduction to the 4 basics of vbscript just as a preamble. They're pretty much the backbone of coding and you'll be using them alot. Hopefully, this will give you a general understanding of how they work, but for extensive script language tutorials you should check out W3Schools.com.

What are the 4 basics of Vbscript?

  • Procedures
  • Variables
  • Conditions
  • Loops


Procedures

Open DesktopX and create a new object. In Object Properties > General, click on ‘new’ script. The next window to appear is the DesktopX Script Editor.

The DesktopX Script Editor
The DesktopX Script Editor

As you can see some codes have already been created. These are called procedures.

There are two types of procedures: subs and functions. As you can see the ones in the DX Script Editor are subs. We know this because they begin with Sub and end with End Sub. It's even color coded!

Functions look pretty much the same way:

What’s the difference between subs and functions, you ask? Well the textbook answer is that functions return a value and subs do not. But we don’t need to go into that just yet.

DesktopX has a lot of procedures you can use. They can be found in the Developer’s Guide here: Object Callbacks

Many of them are run or ‘called’ based on user interaction or object interaction. Like the OnScriptEnter sub. It is called only when the object first runs or when the script is applied.

You can always write your own procedures, but they have to be called from one of the DesktopX procedures. To do so, you’d simply insert this line: Call NameOfProcedure. If I wanted my procedure to run on start up I'd call it from the OnScriptEnter sub like this:

'Called when script is executed
Sub Object_OnScriptEnter
	Call mySub
End Sub

Variables

What is a variable? Variables are ways of storing information. They are written in name/value pairs like this:

name = value
mynick = "sViz"
age = 21
something = "somethingelse"
etc = "andsoforth"

Name, mynick, age, something, and etc are all variables. The only rules about variables is that the name has to start with a letter and it cannot contain a period(.). Variables can be recognized within the procedure they’re created (local) or they can be recognized from anywhere in your script (global). “What’s that matter?”, you might wonder. Well if a variable is local it can have one value in this procedure and another value in another procedure. If a variable is global, it can only have one value that is shared in all procedures. To recognize a variable globally you have to declare it at the top of your script (outside of any procedure) like this:

So even though the variable is created in OnScriptEnter, mySub will still recognize the variable and its value. (See Tips for declaring several variables)


Conditionals

Conditional statements allow different actions for different conditions. What if my age was 22 or 23? We can use conditionals to take different actions for the different ages. There are 4 types of conditionals:

  • If Then
  • If Then Else
  • If Then ElseIf
  • Select Case

Let’s see how they are written:

IF THEN

Sub mySub
	If age = 21 then msgbox "sViz is 21 years old"
	If age = 22 then msgbox "sViz is 22 years old"
	If age = 23 then msgbox "sViz is 23 years old"
End Sub

Here we’ve defined 3 different actions to take for three different conditions.

CONDITION: If age = 21 then

ACTION: msgbox "sViz is 21 years old"


Writing three IF THEN statements is the same as writing an IF THEN ELSEIF statement.

IF THEN ELSEIF

Sub mySub
	If age = 21 then
		Msgbox "sViz is 21 years old"
	ElseIf age = 22 then
		Msgbox "sViz is 22 years old"
	ElseIf age = 23 then
		Msgbox "sViz is 23 years old"
	End If
End Sub

We list each condition followed by the action and then we always close the conditional statement with End If. This is also the same as writing a SELECT CASE statement.

SELECT CASE

Sub mySub
	Select Case age
		Case 21
			Msgbox "sViz is 21 years old"
		Case 22
			Msgbox "sViz is 22 years old"
		Case 23
			Msgbox "sViz is 23 years old"
	End Select
End Sub

CONDITION: Case 21

ACTION: Msgbox "sViz is 21 years old"

After listing each condition and their action we always close with End Select

IF THEN ELSE

Sub mySub
	If age = 21 Then 
		msgbox "sViz is 21 years old"
	Else
		Msgbox "sViz is older than 21"
	End If
End Sub

Here we are limited to just two conditions: If age = 21 and if it does not = 21. We have only two actions. So if age = 21 the message box will read “sViz is 21 years old”. If age = 22 or 23 or 50 or 900, for that matter, the message box will always read “sViz is older than 21”.

You can also combine it with an IF THEN ELSEIF statement but the ELSE statement must be listed last:

Sub mySub
	If age = 21 Then
		Msgbox "sViz is 21 years old"
	ElseIf age = 22 Then
		Msgbox "sViz is 22 years old"
	ElseIf age = 23 Then
		Msgbox "sViz is 23 years old"
	Else
		Msgbox "sViz is older than 23"
	End If
End Sub


Loops

Loops are statements that are run in a loop for a specified amount of times. They will perform whatever action you assign them for said amount of times.

There are 4 types of loops:

  • For Next
  • For Each
  • Do Loop
  • While Wend

We’ll only be dealing with the two most common loops for now: For Next and For Each

FOR NEXT

Sub mySub
	For i = 1 To 3
		Msgbox "Message " & i
	Next
End Sub

This loop is run three times starting from 1, so it will display a message box three times. The letter ‘i’ you see there is also a variable and its value is the number on the other side of the equal sign. So on the first loop i = 1, on the second loop i = 2, on the third loop i = 3. Because it is a variable we can use it elsewhere in the procedure like in the message box. So on the first loop the msgbox will read “Message 1”, on the second “Message 2”, and on the third “Message 3”. You can start from 0, but just remember 0 to 3 will actually be 4 loops (0,1,2,3).

FOR EACH

For Each is a loop that performs actions for each item in a collection. Let’s have a look

Sub mySub
	For Each elem In DesktopX.Objects
		Msgbox elem.name
	Next
End Sub

This loop will display a message box for each object (elem) in the DesktopX.Objects collection. The message box will display the name of the object (elem.name). So, if there are 3 objects in DesktopX.Objects, three message boxes will be displayed.


That's it! Hopefully this tutorial has helped grease the wheels a bit and you'll have a general understanding for the composition of further scripts. Now that we’ve covered the 4 basics, we're ready to start DXScripting!

Next Tutorial: DesktopX: Scripting Visibility (Beginner)

Tips

  • To declare several variables you can list them using commas:
Dim age, mynick, var3

Or you can list them separately:

Dim age
Dim mynick
Dim var3

Further Reading