DesktopX: Scripting Basics
From WinCustomize Wiki
| Learning DX Step-By-Step - #4: Scripting 101 | ||||||||
| ||||||||
Contents |
[edit] Introduction
Today's Lesson: "Scripting 101"
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.
[edit] Scripting 101
[edit] 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
[edit] 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.
- 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)
[edit] 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:
[edit] 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 wherever there is an apostrophe ( ' ), the line that follows is only Comments, not code.
'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
[edit] 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:
'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
[edit] Conclusion
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




