Learning DX Step-By-Step - #4

Tutorial #4 - Scripting 101

Sunday, February 25, 2007 by RomanDA | Discussion: DesktopX Tutorials

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 xd,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")
  xd = 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(xd),1) = "d" Then xd = sx(1)&s2&sx(0)&s2&sx(2) Else xd = sx(0)&s2&sx(1)&s2&sx(2)
  FormatDate = FormatDateTime(xd,2)
  Set xd = 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
First Previous Page 2 of 2 Next Last
thomassen
Reply #21 Wednesday, February 28, 2007 5:29 AM
'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.
RomanDA
Reply #22 Wednesday, February 28, 2007 10:28 AM
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.
Uvah
Reply #23 Wednesday, February 28, 2007 7:01 PM
You got that right. These gadgets fascinate me to no end. First chance I get I'm going to give it a go
bobbyhundreds
Reply #24 Monday, April 09, 2007 8:35 AM
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.  
ZubaZ
Reply #25 Monday, April 09, 2007 8:49 AM
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. 
bobbyhundreds
Reply #26 Monday, April 09, 2007 9:50 AM
System settings are for UK in my regional settings but when I try this script the date is ok until I add the time to it,Then it changes to the U.S. format (month before day).
RomanDA
Reply #27 Monday, April 09, 2007 11:45 AM
Working with dates is a major problem.
I have this problem with my Holiday Countdown pro as well. There are some way to check for this, but its going to be an issue with all date related programs.

VAD_M made a great function:

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

It will take whatever format is fed to it and convert it to the correct regional date format.

usage:
userdate = "11/02/2007"
mydate = FormatDate(USERDATE)


mydate would return 2/11/2007 or 2.11.2007 etc. It seems to work for me, but you might have to play with it to get it to work how you want.

Hope this helps
bobbyhundreds
Reply #28 Tuesday, April 10, 2007 11:14 AM
Working with dates is a major problem.
I have this problem with my Holiday Countdown pro as well. There are some way to check for this, but its going to be an issue with all date related programs.

VAD_M made a great function:


Great,thanks Roman,that did the trick.
By the way,thanks very much for these tutorials,I'm a total novice at this but you have speeded up my learning time a great deal. Cheers !  
RomanDA
Reply #29 Tuesday, April 10, 2007 1:44 PM
Im glad they are helping.. thats the whole idea.. look forward to seeing your work.
Alternate Setting
Reply #30 Sunday, May 06, 2007 11:33 AM
Hi RomanDA,

Appreciate a little extra help as 'Step 4' isn't working as expected. Should the script box contain anything other than the cut & paste code ?

If so, would it be possible to present either a screeny or text print of the final code as it should appear? I've tried a few variations but haven't hit on the right one yet.

The date object works, I've also ran it as a time object just to double check that nothing is stopping the time being called up. Steps 1 to 3 are fine. I just can't seem to get the cut & paste script to run.

Tks

Error is:-
Line: 7
Code:
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
RomanDA
Reply #31 Tuesday, May 08, 2007 7:21 AM
Hey all found a bug in the way the code is shown above (will redo later)

above it shows:
Sub Object_OnTimer1
CurTime = formatdatetime(now,3) object.text = "Date: " & date & vbnewline & "Time: " & CurTime
End Sub

it SHOULD show:

Sub Object_OnTimer1
CurTime = formatdatetime(now,3)
object.text = "Date: " & date & vbnewline & "Time: " & CurTime
End Sub

There are 2 commands in there, Setting CurTime and setting the Object.text

As for implementing VAD Date Function into this try:
(add the code from post 27 to the END of your script - after all other code blocks)
then update the info above to:

Sub Object_OnTimer1
CurTime = formatdatetime(now,3)
object.text = "Date: " & FormatDate(date) & vbnewline & "Time: " & CurTime
End Sub

That should format the date correctly. No way to test it here without messing up my system. Let me know if this all works.



RomanDA
Reply #32 Tuesday, May 08, 2007 7:39 AM
I have updated the above tutorial, hopefully it all works right now, no matter where you live.
Alternate Setting
Reply #33 Tuesday, May 08, 2007 7:50 AM
Thanks RomanDa

It now works as expected - without the added function the object runs both date & time (counting down the seconds). The date is in my local format - day > month > year.

When I add the function as presented and the adjusted code - the object works as expected. However the format becomes Americanised for me. It becomes month > day > year.
RomanDA
Reply #34 Tuesday, May 08, 2007 8:09 AM
interesting... at least it works.
samhall8731
Reply #35 Wednesday, September 10, 2008 5:02 PM
Do you have a printable version of these tutorials. I tried but they don't come out correctly. The blue panels around the yellow type do not print at all.
RomanDA
Reply #36 Wednesday, September 10, 2008 6:44 PM

Stardock has put all of these into PDFs as well as html here:

Stardock Tutorials

Click the "Tutorial" button

Please login to comment and/or vote for this skin.

Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:

  • Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
  • Access to a great community, with a massive database of many, many areas of interest.
  • Access to contests & subscription offers like exclusive emails.
  • It's simple, and FREE!

Articles Filters

Category:
View:
Search:
Apply



walnut3