DesktopX: Scripting Time Displays (Beginner)

From WinCustomize Wiki

Jump to: navigation, search


Scripting Time Displays
Original Author:sViz
Date Created:September 16, 2007
Application:DesktopX
Programs Used:DesktopX


Contents

[edit] Time Displays

At some point in time you might just run across the need to keep track of the time, whether it’s the actual time of day or the time between object tasks. Scripting offers a way to handle all your time tracking and time displaying needs. This is all achieved with the OnTimer sub procedure.

In this tutorial I’ll show you simple scripts to make the following:

  • Time display
  • Date display
  • Timer display
  • Stopwatch display

Create a text object and apply these settings: font style- bold, font size- 12, font color- white, font border color- black, shadow- enabled.

Image:Dx timetext.png

[edit] Time

DesktopX already has plugins for displaying the time and date so you might just want to use those. If you need added functionality, perhaps for intricate time displays across several different objects, or just as reference for some other function like a calendar widget, I’ve got you covered.

Here is a link to W3Schools on how to write the many forms of Date/Time functions: W3Schools Vbscript

Creat a new script.

First we set the timer:

'Called when the script is executed
Sub Object_OnScriptEnter
	'Set 1-second timer
	object.SetTimer 1,1000
End Sub

(Note: The 1 in object.settimer 1,1000 is the identifier number. So if you have several timers running at once you’d give them different identifier numbers and that’s how the timers are distinguished. 1000 equals one-thousand milliseconds or one second.)

The easiest way to get the time is to use the Time function.

Sub Object_Ontimer1
	Object.text = Time
End Sub

It will display the system time. The format of the time display (12-hr clock or 24-hr clock) will depend on where you live and your system settings. As a personal preference, I think it is better practice to get used to FormatDateTime as it provides more precise time displays that are easier to adjust to your preferences.

Sub Object_OnTimer1
	curTime = FormatDateTime(Now(),0)
	Object.text = curTime
End Sub

You don’t have to make it a variable first like I did. FormatDateTime handles dates or times. The first information in the parenthesis—Now()—is where you specify the date or time to be formatted. The second part, the number after the comma, specifies the type of format. 0 will display the date and time, 1 displays just the full date, 2 displays the short date, 3 displays the full time, 4 displays the short time.

Now if you need to know or display only parts of the time like seconds or the hour you can do that too:

Sub Object_OnTimer1
	curHour = Hour(now)
	curMin = Minute(now)
	curSec = Second(now)
	Object.text = curHour
End Sub

(Check out the link above for more formats.)


[edit] Date

Dates are handled pretty much the same way

For a simple display of the current system date:

Sub Object_OnTimer1
	Object.text = Date
End Sub

For different formats:

Sub Object_OnTimer1
	curTime = FormatDateTime(now(),1)
	Object.text = curTime
End Sub

However, when using FormatDateTime with Date, the only formats that will work correctly are 1 & 2.

For different pieces of information here’s a script with several variables for different formats. Just change which one the object text displays:

Sub Object_OnTimer1
	date1= FormatDateTime(now,1)'--Full date
	date2= FormatDateTime(now,2)'--Short date
	date3= Day(date)'--Current day of the month
	day1= WeekdayName(Weekday(Date), False)'--Full weekday
	day2= WeekdayName(Weekday(Date),True)'--Abbreviated weekday
	day3= Weekday(now)'--Number representing day of week
	currentmonth1= MonthName(Month(now),False)'--Full month
	currentmonth2= MonthName(Month(now),True)'--Abbreviated month
	currentmonth3= Month(now)'--Number representing month of year
	currentyear= Year(now)'--Current year
	Object.text = day1
End Sub

[edit] Timer

Here, we’ll create a 1-second timer that counts down from a specified number and stops once it reaches zero.

First we set the timer in OnScriptEnter and declare our variables:

Dim t

'Called when the script is executed
Sub Object_OnScriptEnter
	'Set 1-second timer
	object.SetTimer 1,1000
	'Set the starting time
	t = 60
	'Set object text
	object.text = ":" & t
End Sub

Now for OnTimer. The first part subtracts from the time until it reaches zero and then it stops the timer. The second part displays the information.

Sub Object_OnTimer1
	
	If t = 0 Then 
		object.KillTimer 1
	Else
		t = t - 1
	End If

	txt = "0"
	If len(t) = 1 Then t = txt & t
	Object.text = ":" & t
End Sub

[edit] Stopwatch

Need to keep track of elapsed time? You may or may not need to display it but here we’ll create a stopwatch display that shows hours, minutes, seconds, and milliseconds. It's basically a timer in reverse. We’ll use l-click to start and stop.

OnScriptEnter, we set our variables and the object text.

Dim milisec, sec, min, hr, stopstart

'Called when the script is executed
Sub Object_OnScriptEnter
	milisec = 0
	sec = 0
	min = 0
	hr = 0
	stopstart = False
	object.text = "Click to start"
End Sub

Now the l-click function. Using the 'stopstart' variable, it checks if the stopwatch is running or not and then starts/stops the timers.

'Called when L-click is released
Function Object_OnLButtonUp(x, y, dragged)
	If Not dragged Then
		If stopstart = False Then
			object.SetTimer 1,1000
			object.SetTimer 2,10
			stopstart = True
		Else
			object.KillTimer 1
			object.KillTimer 2
			stopstart = False
		End If
	End If
End Function

To keep track of the time I used 2 timers; this was because of some issues I was having with milliseconds. Timer 1, the 1-second timer, counts up and resets seconds, minutes and hours. It also automatically sets milliseconds to zero.

Sub Object_Ontimer1
	milisec = 0
	sec = sec + 1
	If sec > 59 Then
		min = min + 1
		sec = 0
	End If
	If min > 59 Then
		hr = hr + 1
		min = 0
	End If
	If hr = 100 Then object.KillTimer 1
End Sub

The first part of timer 2, the 10-millisecond timer, deals with keeping track of the elapsed milliseconds. It counts up and resets the milliseconds. The second part deals with the display, adding a zero to single digits.

Sub Object_OnTimer2
	'--Track elapsed time--
	If milisec > 999 Then
		milisec = 0
		sec = sec + 1
	Else
		milisec = milisec + 10
	End If
	'--Display elapsed time--
	txt = "0"
	If len(milisec) = 1 Then 
		milisec = txt & txt & milisec
	ElseIf len(milisec) = 2 Then
		milisec = txt & milisec
	End If
	If len(sec) = 1 Then sec = txt & sec
	If len(min) = 1 Then min = txt & min
	If len(hr) = 1 Then hr = txt & hr
	
	Object.text = hr & ":" & min & ":" & sec & ":" & milisec
End Sub

This concludes today's lesson. :)

Next Tutorial: DesktopX: Scripting A Complete Widget 1

[edit] Tips

  • To reset the stopwatch you only need to create a procedure that clears all the variables, setting them to 0.
  • The 10-millisecond timer isn't as accurate as it should be but the 1-second timer keeps everything on time pretty well.

[edit] Further Reading