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 1 of 2 Next Last
Bichur
Reply #1 Sunday, February 25, 2007 2:49 PM
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!!!

thomassen
Reply #2 Sunday, February 25, 2007 4:10 PM
Wow, you are really turning out those tutorials in a fast pace. Good work.
p.s. minor typo in the sub-heading.
Bichur
Reply #3 Sunday, February 25, 2007 4:16 PM
p.s. minor typo in the sub-heading.


Not so, as it applies to DexktopX
RomanDA
Reply #4 Sunday, February 25, 2007 5:10 PM
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.
thomassen
Reply #5 Sunday, February 25, 2007 5:21 PM
I think one on timers would be good. Seem to be quite a few DX questions which realted to that in some way.
RomanDA
Reply #6 Sunday, February 25, 2007 5:24 PM
(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.
Bichur
Reply #7 Sunday, February 25, 2007 5:35 PM
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
RomanDA
Reply #8 Sunday, February 25, 2007 5:47 PM
Bichur,
Glad your taking a chance an trying these things out, its actually a lot easier then its made to look.
Bichur
Reply #9 Sunday, February 25, 2007 6:35 PM
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

RomanDA
Reply #10 Sunday, February 25, 2007 6:42 PM
printing my Tuts? where
Bichur
Reply #11 Sunday, February 25, 2007 6:44 PM
link above
Bichur
Reply #12 Sunday, February 25, 2007 6:44 PM
normal, open browser, print
RomanDA
Reply #13 Sunday, February 25, 2007 6:49 PM
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.
Bichur
Reply #14 Sunday, February 25, 2007 9:53 PM
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
vStyler
Reply #15 Sunday, February 25, 2007 11:51 PM
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*
RomanDA
Reply #16 Monday, February 26, 2007 3:56 PM
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"
vStyler
Reply #17 Monday, February 26, 2007 3:59 PM
  
ZubaZ
Reply #18 Monday, February 26, 2007 5:03 PM
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. 
RomanDA
Reply #19 Monday, February 26, 2007 6:17 PM
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....
Uvah
Reply #20 Wednesday, February 28, 2007 2:33 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.

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



walnut2