Learning DX Step-By-Step - #4

Tutorial #4 - Scripting 101

By on February 25, 2007 1:42:50 PM from WinCustomize Forums WinCustomize Forums

RomanDA

Join Date 12/2003
+147
Step-by-Step Tutorials

#4 - Scripting 101

A series by RomanDA

Listing of other DX Tutorials: Click here

Today's Lesson: "Scripting 101"  - UPDATED 5/08/07

Today we will learn how to do some VERY basic scripting in DexktopX. Scripting in DX can be done with JavaScript as well as VBScript, but since i know next to nothing about Javascript, I will be covering VBScript.

Please Look over the previous 3 tutorials so that we can skip up to some things here without redoing everything we have already done. Things like Creating a new object, and changing font sizes, etc. I will not cover those again, if you haven't look at the first 3 you will need to go back and check them now.

For this and all the Step-By-Step DX Tutorials you will need to purchase DesktopX for $14.95 from Stardock.

Lets get started.

STEP 1 - Create the object


Like I stated above, I am not going to walk you thru everything here, so please look over the other tutorials first.
  • Create a new object (see Tutorial #1)
  • Make it a TEXT object.
  • Make the text large enough you can see it on your desktop
  • Add the text DATE: to the object. (you can make it whatever color you want, and font)
  • RIGHT-CLICK on the new object and select PROPERTIES
STEP 2 - Making your first Script

  • From the PROPERTIES window click on the NEW Button, next to SCRIPT.
  • You will see the above screen (DX always creates a OnScriptEnter and OnScriptExit subs.
  • The ENTER is what runs when you load up the object for the first time.
  • The EXIT is what runs when you close down your object.
  • In this case we are going to make the object show the current date when we load it up
  • We are going to add a single line of code in the OnScriptEnter Section:
    • object.text = date
    • As you type in this info you will see dropdowns that show the options you can use.

       All you need to do is click on T in TEXT and this dialog will show up, so you can hit enter as soon as the dropdown jumps to Text, or continue typing.
  • That's it, lets cover what we just did.
    • object. - this is how DX refers to the current object, so that it knows what object we are working on.
    • text - this is the actual text of the object.
    • = - is simply telling it that the text EQUALS what comes next
    • date - the date is a built in function of visual basic. It is simply they current date.
    • combining it all, it takes the current object's text and makes it equal to the current date.
  • Once this is all entered, click on FILE then SAVE AND CLOSE Editor
  • Then click on OK and your TEXT on the desktop should show:
    (ok, it will show the REAL date, not the date I made this - lol)
STEP 3 - Adding some more info to the script

Hopefully the above wasn't to confusing, it worked, and your ready to add a little more to it.

  • RIGHT-CLICK on the the object and in the menu you will now have a few different options
  • You will want to click on EDIT SCRIPT
  • You should see the same EDIT window you saw above.
  • Let's add some text to the item.
  • edit the line object.text = date
  • to look like: object.text = "Date: " & date
  • What this does is put the text Date: before the date itself. the & is used to join the 2 items.
  • click on FILE then SAVE AND CLOSE Editor
  • Then click on OK and your TEXT on the desktop should show:
STEP 4 - Lets add some TIME

We are going to make this into a time object, with the current time being displayed at all times. To do this we need to add a TIMER to the object. Timers are just what they sound like, they perform an action at set intervals. You can have multiple timers running at the same time, they can be at different intervals. Lets see how this looks in the code:

  • RIGHT-CLICK on the object and select EDIT SCRIPT
  • We need to move some things around, please copy/paste the below code into your object.
  • Note the yellow text is only Comments, that's why they have a ' before them.
'Called when the script is executed
Sub Object_OnScriptEnter
  Object.SetTimer 1, 1000

  '-- This turns on a timer called 1000 and runs what's in there every 1,000 milliseconds or 1 second

End Sub

Sub Object_OnTimer1
'-- This is the Timer we started above, notice the 1 is the same as the 1, in our SetTimer call? '--- FormatDateTime(now,3) returns the complete time in the format: hh:mm:ss am/pm
 CurTime = formatdatetime(now,3)
 object.text = "Date: " & date & vbnewline & "Time: " & CurTime

'The vbnewline is a linefeed/break putting the time on a line UNDER the date

End Sub

'Called when the script is terminated
Sub Object_OnScriptExit
 object.KillTimer 1
'--- This shuts the timer off when the widget closes
End Sub

  • click on FILE then SAVE AND CLOSE Editor
  • Then click on OK and your TEXT on the desktop should show:
  • There will be a LAG of 1 second before this shows on the screen because it has to wait that 1 second before the Timer fires off. You can remove this lag by adding the following line right below the object.settimer line, so the Object_OnScriptEnter would look like this:
'Called when the script is executed
Sub Object_OnScriptEnter
 Object.SetTimer 1, 1000
 Call Object_OnTimer1
End Sub
 

Update for other Date Formats:


There are a lot of places in the world where the date isn't in the format mm/dd/yyyy.  To handle that, VAD_M has created a great function that will convert any date format to the current;y selected format of your system.

Lets show what our code looks like if we add VAD_M's code:

'--- NEW CODE or CHANGE

'Called when the script is executed
Sub Object_OnScriptEnter
  Object.SetTimer 1, 1000

  '-- This turns on a timer called 1000 and runs what's in there every 1,000 milliseconds or 1 second

End Sub

Sub Object_OnTimer1
'-- This is the Timer we started above, notice the 1 is the same as the 1, in our SetTimer call? '--- FormatDateTime(now,3) returns the complete time in the format: hh:mm:ss am/pm
 CurTime = formatdatetime(now,3)
 object.text = "Date: " &
FormatDate(date) & vbnewline & "Time: " & CurTime
'The vbnewline is a linefeed/break putting the time on a line UNDER the date

End Sub

'Called when the script is terminated
Sub Object_OnScriptExit
 object.KillTimer 1
'--- This shuts the timer off when the widget closes
End Sub

Function FormatDate(xdate)
  On Error Resume Next
  Dim ,s1,s2,sx
  If instr(xdate,"/") > 0 Then
   s1 = "/"
  ElseIf instr(xdate,".") > 0 Then
   s1 = "."
  ElseIf instr(xdate,"-") > 0 Then
   s1 = "-"
  End If
  Set objShell = CreateObject("WScript.Shell")
  = objShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\sShortDate")
  s2 = objShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\sDate")
  Set objShell = nothing
  sx = split(xdate,s1)
  If left(LCase(),1) = "d" Then = sx(1)&s2&sx(0)&s2&sx(2) Else = sx(0)&s2&sx(1)&s2&sx(2)
  FormatDate = FormatDateTime(,2)
  Set = nothing
  Set s1 = nothing
  Set s2 = nothing
  Set sx = nothing
End Function

CONCLUSIONN


Ok, you now have a working date/time clock on your desktop now.. woo hoo.. LOL I hope this first step into scripting in DX has shown you how simple it can be. I will continue to work on scripts for the next 2-3 tutorials, mainly the basics, if you want more complicated Tutorials look over my older ones on the index page.

I know this is some very basic stuff, but that is the idea, most people are afraid of scripting, and I want to show that anyone can get in there and at least get started on scripting.

I hope you have enjoyed this step into DX, and look forward to the next installment..

Enjoy,
RomanDA
AKA: David A. Roman
http://romanda.wincustomize.com
36 Replies
Search this post
Subscription Options


Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 2:49:38 PM from WinCustomize Forums WinCustomize Forums
Very Nice.
I'm not sure what level you are doing these for, so from a non experienced level (me) I just want to point out (so far):
from step two:
We are going to add a single line of code in the OnScriptEnter Section:

you're inputting this on line 3 (I've never know if the script box was an example of a sort or was an actual starting place.) I've never tried coding - always used a copy/paste from other posts and such without knowing what I'm doing.

On the plus side (for me) I actually figured it out before the 'copy/paste' in step 4.


Keep'em coming!!!

Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 4:10:57 PM from WinCustomize Forums WinCustomize Forums
Wow, you are really turning out those tutorials in a fast pace. Good work.
p.s. minor typo in the sub-heading.
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 4:16:11 PM from WinCustomize Forums WinCustomize Forums
p.s. minor typo in the sub-heading.


Not so, as it applies to DexktopX
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 5:10:54 PM from WinCustomize Forums WinCustomize Forums
I'm not sure what level you are doing these for, so from a non experienced level (me) I just want to point out (so far):
from step two:


Im trying to aim for someone who has never done ANYTHING in DX at all. Its hard to do, its like trying to explain to someone how to ride a bike, when you just "do it" lol.

Sub-heading is fixed.. thanks..

Im trying to work on 1 every 2-3 days (if i can get time).

I really need to get some ideas on what people are looking for in the way of "BASIC" scripts.
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 5:21:07 PM from WinCustomize Forums WinCustomize Forums
I think one on timers would be good. Seem to be quite a few DX questions which realted to that in some way.
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 5:24:30 PM from WinCustomize Forums WinCustomize Forums
(I've never know if the script box was an example of a sort or was an actual starting place.) I've never tried coding - always used a copy/paste from other posts and such without knowing what I'm doing.


When you hit "NEW" on any object (for the script) it creates a template like this one:

'Called when the script is executed
Sub Object_OnScriptEnter

End Sub

'Called when the script is terminated
Sub Object_OnScriptExit

End Sub

It even states above each SUB what it does "Called when the script is executed" or "terminated".

That means exactly what it says, those 2 sections are the SUBs that run on start and exit. A good bit of scripts are setup in the "OnScriptEnter" section, as they need to run when the object/widget loads.

I also covered the Timer sub as well in this one. That gets used a lot. There are tons of functions, some run when you move your mouse over things, others when you click on an object, etc. You just have to know what you want it to do.

You can make your own functions and subs, that do repetitive tasks, etc. I will cover these in another Tutorial.

Scripting in DX can be very simple, to amazingly complex.
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 5:35:01 PM from WinCustomize Forums WinCustomize Forums
It even states above each SUB what it does "Called when the script is executed" or "terminated".


understood that part - but having never (repeat never) having scripted on my own - I just thought that needed to go away and be replaced - not realizing you could leave and edit in between - if that makes sense

but using your tutorial and then looking at the copy/paste provided, it becomes clearer
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 5:47:09 PM from WinCustomize Forums WinCustomize Forums
Bichur,
Glad your taking a chance an trying these things out, its actually a lot easier then its made to look.
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 6:35:56 PM from WinCustomize Forums WinCustomize Forums
Just saw a post about printing your Tutes... doesn't seem to be working.

If I just preview the print it expands to 3 pages - every other is blank and only get part of Step 2.

Any chance of getting a pdf or printable version? Or would you like a pdf version?


link to print post

Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 6:42:37 PM from WinCustomize Forums WinCustomize Forums
printing my Tuts? where
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 6:44:15 PM from WinCustomize Forums WinCustomize Forums
link above
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 6:44:40 PM from WinCustomize Forums WinCustomize Forums
normal, open browser, print
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 6:49:47 PM from WinCustomize Forums WinCustomize Forums
that is really strange, i have never tried to print anything on here before.

I can help.. can you email me.. i dont want to make PDF files, but i can point you to somewhere else to look at the pages.
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 9:53:16 PM from WinCustomize Forums WinCustomize Forums
that's okay - if you don't want pdf, no matter (i was offering since I have acrobat7)

i found i could go to file > edit with word > then save as and it gives me copy

i can't create so i learn to workaround
Reason for Karma (Optional)
Successfully updated karma reason!
February 25, 2007 11:51:26 PM from WinCustomize Forums WinCustomize Forums
good job on the tuts RomanDA..very unselfish of you.

unfortunately codeing makes me feel funny and want to lay down.   

jumps back into SKS*
Reason for Karma (Optional)
Successfully updated karma reason!
February 26, 2007 3:56:22 PM from WinCustomize Forums WinCustomize Forums
very unselfish of you.


LOL.. now.. where is ZU.. he would say...
"no its not, romanda is running out of people to steal code from"
Reason for Karma (Optional)
Successfully updated karma reason!
February 26, 2007 3:59:22 PM from WinCustomize Forums WinCustomize Forums
  
Reason for Karma (Optional)
Successfully updated karma reason!
February 26, 2007 5:03:46 PM from WinCustomize Forums WinCustomize Forums
Some might think that romanda was teaching to help the people.
Some might think that he was trying build synergy to work with people and lift up the whole community
Some might think that he wanted to get people stirred up so he could steal from them later

Me, I know RomanDA, and I believe this he is merely showing off how much better he is than everyone else. 
Reason for Karma (Optional)
Successfully updated karma reason!
February 26, 2007 6:17:59 PM from WinCustomize Forums WinCustomize Forums
Me, I know RomanDA, and I believe this he is merely showing off how much better he is than everyone else.


yes.. thats my plan.. spend 2-3hrs a tutorial showing people how to make things on their own, so i can look like i know soo much more then they do.. yep.. you busted me....
Reason for Karma (Optional)
Successfully updated karma reason!
February 28, 2007 2:33:07 AM from WinCustomize Forums WinCustomize Forums
I've never scripted a thing. I don't even know if I have VBscript. Do I have to download it? These TUTs are so cool and I'd love to learn how. But gots to have the tools first.
Reason for Karma (Optional)
Successfully updated karma reason!
February 28, 2007 5:29:18 AM from WinCustomize Forums WinCustomize Forums
've never scripted a thing. I don't even know if I have VBscript. Do I have to download it? These TUTs are so cool and I'd love to learn how. But gots to have the tools first.

These tutorials are for VBScript within DesktopX.

However, if you want to learn just VBScript then head over to http://www.w3schools.com/vbscript/default.asp. They got loads of other good tutorials there as well.
VBScript and JScript is part of the Windows Script engine which usually is preinstalled on your machine. If not, you can get it here: http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=C717D943-7E4B-4622-86EB-95A22B832CAA.

People mostly use VBScript when they create gadgets in DesgtopX. However, if you are completely new to scripting, consider JScript as you will then be able to write Javascript for your website. Also other gadget/widget applications use Javascript + DHTML to power the gadgets.
Reason for Karma (Optional)
Successfully updated karma reason!
February 28, 2007 10:28:31 AM from WinCustomize Forums WinCustomize Forums
I've never scripted a thing. I don't even know if I have VBscript. Do I have to download it? These TUTs are so cool and I'd love to learn how. But gots to have the tools first.


thomassen is right, all you really need here is to download DesktopX, but you need the version that allows you to create objects, not just load objects. There are several version of DX. I have included a link at the top of every tutorial to the right version of DX. Enjoy.

Im glad you are enjoying these... they are more fun when you follow them on your system.
Reason for Karma (Optional)
Successfully updated karma reason!
February 28, 2007 7:01:27 PM from WinCustomize Forums WinCustomize Forums
You got that right. These gadgets fascinate me to no end. First chance I get I'm going to give it a go
Reason for Karma (Optional)
Successfully updated karma reason!
April 9, 2007 8:35:53 AM from WinCustomize Forums WinCustomize Forums
How do I format the Date/times please? In the UK we write the day before the month but I can't figure out how to change the format to this.
Thanks for the tutorial by the way.....excellent.  
Reason for Karma (Optional)
Successfully updated karma reason!
April 9, 2007 8:49:29 AM from WinCustomize Forums WinCustomize Forums
This script should grab the setting from Windows.  Are they not?

Go to Control Panel and the Regional and Language settings.  Adjust there and come on back to report. 
Reason for Karma (Optional)
Successfully updated karma reason!
Stardock Forums v1.0.0.0    #108432  walnut2   Server Load Time: 00:00:00.0001109   Page Render Time: