DesktopX: Scripting Groups (Beginner)

From WinCustomize Wiki

Jump to: navigation, search
Scripting Controlling Groups
Original Author:sViz
Date Created:October 6, 2007
Application:DesktopX
Programs Used:DesktopX


In this tutorial we’ll look at how to control groups through scripting, and how to control several objects as a group even if they are not grouped.

Contents

[edit] Groups and Children

Create some objects, group them, and name the group “testgroup”. Create a separate object which will be used as the group controller. This is where we’ll put the scripts.

Image:Dx groupsetup1.png


When scripting groups, the For Each statement is extremely useful. This statement basically tells DX to do something for each object in the group. In this statement each object is called ‘elem’ and you can assign it most normal properties (e.g. object.text / elem.text, object.hue / elem.hue etc.). Child objects are just like groups so you can use the same method.


Here is a script that toggles the visibility of a group on left click. Insert it into the “controller” object:

Sub Object_OnLbuttonUp(x,y,dragged)
	If Not dragged Then 
		For Each elem In Desktopx.GroupObjects("testgroup")
			If elem.visible = True Then 
				elem.visible = False
			ElseIf elem.visible = False Then 
				elem.visible = True
			End If
		Next
	End If
End Sub

Here’s one you’d use to toggle child objects:

Sub Object_OnLbuttonUp(x,y,dragged)
	If Not dragged Then 
		For Each elem In Desktopx.Object(“Parentobject”).Children
			If elem.visible = True Then 
				elem.visible = False
			ElseIf elem.visible = False Then 
				elem.visible = True
			End If
		Next
	End If
End Sub

[edit] Ungrouped Collection of Objects

Now, what if you don’t want your objects in a group? After all you might want them to move independently. What if they’re not child objects but you still want to control several at the same time? There are two methods I use for this. One lists each object, and the other uses the For Next loop.

Ungroup the grouped objects and insert the scripts below into the “controller”.

[edit] Method 1: Listing each object

'Called when L-click is released
Function Object_OnLButtonUp(x, y, dragged)
	If Not dragged Then
		'Check if one of the objects is already hidden or showing
		Select Case Desktopx.Object("1").visible
			Case True
				showhide= False
			Case False
				showhide= True
		End Select
		'Set objects visibility
		Desktopx.Object("1").visible = showhide
		Desktopx.Object("2").visible = showhide
		Desktopx.Object("3").visible = showhide
	End If
End Function

In this method we would check the visibility of one of the objects and use that to set a variable. After, we set the visibility of each object. I use this method if I have a small number of objects I want to control.

[edit] Method 2: For Next

Obviously, when you think about it, if you had alot of ungrouped objects you wanted to control, listing each of them would be laborious and tweaking one small aspect would be painstaking. Trust me, I know. Before I got the hang of scripting I used this method ad nauseam and every time I wanted to make a change I had to go through the entire list—several times! There is an easier way. This method uses the For Next statement and is facilitated by how you name the objects you want to control.

First, rename your objects like this: object1, object2, object3 OR item1, item2, item3 (not item01, 02, 03 etc.)

Image:Dx groupsetup2.png

Whatever name you use make sure you name the other objects the same and in serial order. This is important. It is also important to define the number of objects you are controlling. We’ll use the variable 'numofobjs' to do this. Insert the script below into the group controller.

Dim numofobjs

'Called when script is executed
Sub Object_OnScriptEnter
	'Define number of objects to control
	numofobjs = 3
End Sub

Sub Object_OnLbuttonUp(x,y,dragged)
	If Not dragged Then toggle
End Sub

Function toggle
	'Check the visiblity of object
	Select Case Desktopx.Object("object1").visible
		Case True
			showhide= False
		Case False
			showhide= True
	End Select
	'Reset visibility of objects
	For x= 1 To numofobjs
		desktopx.Object("object" & x).visible =showhide
	Next
End Function

That's all!

Next Tutorial: DesktopX: Scripting Images (Beginner)

[edit] Tips

None right now. :)

[edit] Further Reading