A Newbie's Guide to DesktopX Scripting, Part 1

Making A simple Rollup Button.

A Newbie's Guide to DesktopX Scripting, Part 1.  (Making A simple Rollup Button.)
 

  In the last few months I have finally had the chance to start learning to do some DesktopX scripting. This is something I have been interested in doing since DesktopX first started supporting scripts. Back in the days before i spent all my time on Icons, I spent it all on DesktopX. I made more Zoomers then I can remember, and several complete themes, all of which need to be updated. But I never got the chance to learn to script.

    One of the major perks to working at Stardock, for me a least, is that I work write next to several great programmers.  This however has awakened a my long set aside itch to learn to code, and not wanting to change carriers at this late point in my life, DesktopX scripting has presented itself as the perfect why for me to scratch it. So with allot of question asking, and allot of looking at other peoples code, mostly Tiggs, _Martin_, and Brazon_Weep. I have started to learn.

    As I began my learning process I was struck by a few things. First, How much is possible with DesktopX when you start adding scripts. Second, how many people write the code to be used only in that one object. The first fact got me addicted to DX scripting in about a day. The second fact, made me want to learn to do it right. As I said, I work with several great programmers, most of which are as anal about coding, as I am about design. So I am constantly observing, what i will call, and I am a coding Newbie, good programming habits. So as I learn I am trying to fallow a few guidelines that I have picked up from them. and they are.
   
    Always, always, comment.  This will help other coders, as well as save you a world of hurt as you look back to see what you did, wrong or write.

    Never Hard Code variables, if at all possible. This helps your code be reusable. For yourself and others.

    Write Modularly. This is just good practice all around. An my main point. of this Tutorial.

    Quickly I will point out, that I am a total newbie, and allot of what I am going to lay out here will probably seem obvious or wrong, to experienced programmers, but this is not for them, this is for people like myself, that love to skin, and always thought, I could do "this" if I new how to script. Well I'm learning, and I thought maybe a few others would want to come along for the ride.

    Now to the, all be it, short and simple script. I did not want to do the Typical "Hello world" sample that you see in every programming book, so I am using a super simple "Rollup" button. That I made yesterday for something I am working on.

    First lets start with the code:
 




Download Sample Object

'****************************************************************************************************************
'*************                                 Sample_Rollup_Button                                  ************
'****************************************************************************************************************


Dim ulRolledUpSize
Dim ulExpandedSize                      
Dim ulSpeed

ulRolledUpSize   = 32                    'This sets the size you want your parent object to rollup to.
ulExpandedSize   = 250                   'This sets default the size you want your parent object to rolled down
ulSpeed          = 20                    'This sets the # of frames you want it to take to role up or down

'****************************************************************************************************************

Sub Object_OnLButtonDown(x,y)                                'When this object is clicked.

    If Object.StatePreempt = "Up" Then                       'Check to see if we are Rolled Down.
        Rollup()                                             'If it is, Call the Rollup sub.
    Else                                                     'If Not
        Rolldown()                                           'Call the Rolldown Sub.
    End If

End Sub


'****************************************************************************************************************

Sub Rollup()
   
   ulExpandedSize = Object.Parent.Object.Height              'Set the current size as the Expanded size

    Object.StatePreempt = "Down"                             'Change the image state to 'down'

    Do While Object.Parent.Object.Height > ulRolledUpSize   'Size the image down, in # of Frames set in ulSpeed
        Object.Parent.Object.Height = Object.Parent.Object.Height - ((ulExpandedSize - ulRolledUpSize) / ulSpeed)
        Sleep 10                                             'Pause for a bit between frames.
    Loop

    Object.Parent.Object.Height = ulRolledUpSize             'Make sure the Final size is Correct.

End Sub

'****************************************************************************************************************

Sub Rolldown()

    Object.StatePreempt = "Up"                               'Change the image state to 'up'

    Do While Object.Parent.Object.Height < ulExpandedSize    'Size the image up, in # of Frames set in ulSpeed
        Object.Parent.Object.Height = Object.Parent.Object.Height + ((ulExpandedSize - ulRolledUpSize) / ulSpeed)
        Sleep 10                                             'Pause for a bit between frames.
    Loop

    Object.Parent.Object.Height = ulExpandedSize             'Make sure the Final size is Correct.

End Sub

'****************************************************************************************************************

 

    Let me quickly point out, that all this code goes in the Rollup/Down Button. However most the time it is effecting its Parent. As I describe what I am doing here I will refer to the Button Object as 'the Button' and the Parent Object as 'The Parent Object' . Note; For some reason, and it may be a bug, at the time I am writing this, you need to have a script in the parent object, in order to call it with Object.parent, it can be, and In this example is, blank.

    This Code is pretty simple, and I will go over it and explain what is happening. The first thing I might Point out, is that I have divided up all the Sub with '******* this is to make it easier to read, and make it seam more modular. In fact it would make no difference to the code if I did not do this. I have also tried really hard to comment as much as possible, this once again is a good practice

    In the first section.
    We Declare all the variables we want to use in the button, by placing them here we are making them public, so all the Subs in the script can see them. (we should talk more about this in a later lesson). Then we Set there initial values. The size we want the Parant Object to rollup to, and down to, as well as the amount of frames we want it to take to do it.

    In the Second Section. 'Object_OnLButtonDown(x,y)'
    When you click on the button, It causes the script to do a simple If statement, checking weather the Rollup button is Rolled up or down. This is a good example of something cool you can do with DX scripting. That is, You can use a objects state as a variable. In this case this Button contains two states 'Up' and 'Down' each is an arrow pointing its described directing. If the arrow is pointing up, then we know the Parent Object needs to rollup. if it is not, we know it needs to roll down.

    In the Third Section. 'Rollup()'
    When we need to Rollup the object we call this. First we get the Parent Objects current size, and save it. This is so this rollup code will work even if you resize the Parent Object. Then we set the Buttons state to 'Down' this makes it so next time it is clicked it will call the rulldown () sub. Then we do a simple While loop. What this dose is shrink the Height of object each time it loops,  it subtracts the a value of the difference between the Rolled up and Rolled Down states, Divided by the # of frames you specified in Section 1. When the size Height becomes less then the Rolled up size it stops. Then Just to be sure we set the Parent Objects size to The Rolled up size.
   
    In the Forth section 'Rolldown()'
    This is the same as the Third section except in reverse, and we don't need to re set the Rolled down size. Hopefully the code will illustrates this clearly

   Well, that about wraps it up for my first attempt to help other newbie's, like myself, learn scripting. This sample is in vbScript, but could have just a simply bin in JavaScript. As I learn more JavaScript I will try and post another example. I hope this helps out a bit. Will be back next week with Part 2.

 

 

8,281 views 11 replies
Reply #1 Top
This is an excellent guide.
Well commented scripts is how I've learnt DX scripts (that and help from Martin when I get stuck ) guides like this makes it even quicker for me to pick up.

Thank you!

Reply #2 Top
Thanks so much for this article. I've been using DX and OD for a long time but am only now beginning to get into scriptiing and so on. My graphic skills stink but maybe I can inspire people to create things.
Reply #3 Top
Very informative.

Thanks. I look forward to future guides.
Reply #4 Top
There's only one thing to keep in mind when making programs modular. VBScript is an interpreted language, and any extra lines of code add to the execution time. One really good way to cut down on CPU use and spped up your scripts is to simply have as few running as possible. For instance, say a widget has 6 objects in it to make up the whole, and 3 of them have scripts. By using pointers to the other objects, you can quite often control all three objects from one script. This will drastically reduce the amount of memeory used by the widget, and use far fewer CPU cycles to accomplish the same thing.

Example of a pointer: You can use this...

DesktopX.Object("SomeObject").Left = X

A better way is to declare a pointer first, like this...

Dim ObjectPointer
Set ObjectPointer = DesktopX.Object("SomeObject")

Then when you want to tell that object to do something, you can use this...

ObjectPointer.Left = X

If you're referring to other objects many times, this will use lots less system resources than using the previous method every time.


In a nutshell, OPTIMIZE OPTIMIZE OPTIMIZE.

VBScript is an 'expensive' language in terms of system resources.
Reply #5 Top
Thanks for the tip, I have just learned about pointers and have not quite got it down when is the best time to use them. However, I did the above sample the way I did, simple to keep it simple, and so you could just slap it onto any object.

As these little lessons move on, we will talk more about optimizing the code. My hope with this example is just to get things started for the Newbies, including myself.
Reply #6 Top
Kewl tutorial! would you mind submiting it to my skinning tutorials website at http://skintuts.tk? there currently arent any scripting tutorials so this would be very useful...
Reply #7 Top
Thanks to Digital Truckers Advice I went back and change a few things in the code, here is the same code but useing a pointer.

'****************************************************************************************************************
'************* Sample_Rollup_Button ************
'****************************************************************************************************************

Dim ulRolledUpSize
Dim ulExpandedSize
Dim ulSpeed
Dim pParent

ulRolledUpSize = 32 'This sets the size you want your parent object to rollup to.
ulExpandedSize = 250 'This sets default the size you want your parent object to rolled down
ulSpeed = 20 'This sets the # of frames you want it to take to role up or down

Set pParent = Object.Parent.Object

'****************************************************************************************************************


Sub Object_OnLButtonDown(x,y) 'When this object is clicked.

If Object.State = "Up" Then 'Check to see if we are Rolled Down.
Rollup() 'If it is, Call the Rollup sub.
Else 'If Not
Rolldown() 'Call the Rolldown Sub.
End If

End Sub

'****************************************************************************************************************

Sub Rollup()

ulExpandedSize = pParent.Height 'Set the current size as the Expanded size
Object.StatePreempt = "Down" 'Change the image state to 'down'

Do While pParent.Height > ulRolledUpSize 'Size the image down, in # of Frames set in ulSpeed
pParent.Height = pParent.Height - ((ulExpandedSize - ulRolledUpSize) / ulSpeed)
Sleep 10 'Pause for a bit between frames.
Loop

pParent.Height = ulRolledUpSize 'Make sure the Final size is Correct.

End Sub

'****************************************************************************************************************

Sub Rolldown()

Object.StatePreempt = "Up" 'Change the image state to 'up'

Do While pParent.Height < ulExpandedSize 'Size the image up, in # of Frames set in ulSpeed
pParent.Height = pParent.Height + ((ulExpandedSize - ulRolledUpSize) / ulSpeed)
Sleep 10 'Pause for a bit between frames.
Loop

pParent.Height = ulExpandedSize 'Make sure the Final size is Correct.

End Sub
'****************************************************************************************************************
Reply #8 Top
very well done,easy to understand..thanks
Reply #9 Top
Good to see you found the time to start scripting at last, mormegil! I remember you were talking about it when I visited.
Reply #10 Top
Might I add this.
To install the downloadable sample object. Empty your "my theme" folder and extract the zip file to it.
Might be better if you were to make the object a dxpack file...

Thanks for the great tips mormegil!
Reply #11 Top
I uploaded it as a DX pack, but for some reason my server is converting it to zip when you download it. All you need to do is change the .zip to .dxpack, and you will be good to go.