DesktopX: Code examples and useful functions - part1 (for power users)

From WinCustomize Wiki

Jump to: navigation, search


Code Examples and Useful Functions Part 1
Original Author:Vad_M
Date Created:September 2, 2007
Application:DesktopX
Programs Used:DesktopX


Note: I'll not draw a pictures here or tell you how create object in DesktopX. I just show a few code examples that may help you in your projects.

Contents

[edit] Using DX Timers In Multifunctional Applications

A few words about using DX timers in the multifunctional applications like system meters:

Of course we can use the integrated DesktopX functions to get full system information or to create any other system meters. This is very comfortable options for beginners but there is one problem. You can't manage the timers which uses in the "integrated" scripts. As a result this may brings about the overweening usage of CPU and (or) System Memory. I have discovered this problem when worked on the first version of System Navigator (LINK).

However this little problem has very simple decision. You need only create your own script with the single (main) timer which will manage all of processes and functions in your widget. The using of CPU and Memory will be minimal in this case.

For example you want get the full information about your system configuration, disk drives as well as the network connections and traffic. If you use the DX meters for this and unite all of these meters in the one widget you will see the abnormal CPU usage. So you need create the Main (parent) Script Object with the single timer which will start the auxiliary (children) scripts on the each of these objects if one of them is active (visible) and will stop all other scripts on the inactive (invisible) objects. As you see this is more than easy!

This is just a several lines with a simple theory that may help you to optimize your future scripts. Someday we will have the detailed conversation about scripting for system meters but now I offer to speak about more simple things.

[edit] Easy Way to Check Gmail

The easy way to check the Gmail incoming e-mails:

You need create one text object (desktopx.object("mymails").text) and a Script Object to apply this code:

Dim username, password

Sub Object_OnScriptEnter
   username = "YOUR GMAIL USER NAME" 
   '(for example if your address is "mike1992@gmail.com" - the user name is just: "mike1992" )  
   password = "YOUR PASSWORD"
   Object.SetTimer 1,(60000*5) '<== this will check your Gmail each 5 minutes 
End Sub
Sub Object_OnTimer1000 '<== main timer
   Call  CheckGmail(username,password,0,"")
End Sub
Sub CheckGmail(un,ps,im,strx)
   On Error Resume Next
   Dim ac,url,objXML,strmail
    If instr(un,"@") > 0 Then ac = split(un,"@")(0)&":"&ps Else ac = un&":"&ps 
   '<this line is need if you forget to extract your user name from Gmail address>
     url = "https://"&ac&"@gmail.google.com/gmail/feed/atom"
   Set objXML = CreateObject("Microsoft.XMLHTTP")
    objXML.Open "GET", url, False
    objXML.send "Authentication"    '<== First of all you should confirm the authenticity of Gmail user
    objXML.Open "GET", url, False
    objXML.send "Gmail Information" '<== Hereon you can retrieve the necessary information
    strx = objXML.responseText
   Set objXML = nothing
   If strx <> "" Then
    If instr(LCase(strx),"fullcount") > 0 Then im = ParseString(strx,"<fullcount>","</fullcount>")
   End If
   If int(im) = 1 Then strmail = "mail" Else strmail = "mails"
    desktopx.object("mymails").text = im&strmail '<== final results
End Sub
Function ParseString(strx,tag1,tag2) '<== this function allow you extract any data from XML string by tags
  Dim x1,x2,xlen                          
   x1 = instr(LCase(strx), LCase(tag1))
   x2 = instr(LCase(strx), LCase(tag2))
  If x1 = 0 Or x2 = 0 Then
   Exit Function  '<== error of parsing...
  Else
   xlen = len(tag1)
   ParseString = mid(strx, x1 + xlen, x2 - x1 - xlen)
  End If
End Function

Sub Object_OnScriptExit
   Object.KillTimer 1 '<== Clear all...
   Set username = nothing
   Set password = nothing  
End Sub

[edit] How to the Get System Uptime

Visualize that you work on a system clock that must show you not only the current time but the system uptime too. No doubt you may not use the integrated DX function for this. But you must do something like this if you want that these both values has been timed:

You need create two text objects:

  • desktopx.object("localtime").text
  • desktopx.object("worktime").text

and one main Script Object where you will put the following code:

Dim uptime

Sub Object_OnScriptEnter
  On Error Resume Next
 'A lot of useful information we can find just in the Windows registry...
  Dim sd,sb
  Set objShell = CreateObject("WScript.Shell") '<== get the default format of short date
    sd = objShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\sShortDate")
  Set objShell = nothing
 'Now we know how our short date looks. Let's use WMI to get the walue of last system reboot.
  Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  Set objSYS = objWMI.ExecQuery("Select LastBootUpTime from Win32_OperatingSystem")
   For Each item In objSYS           
    sb = item.LastBootUpTime        '<== get the value of last system reboot
    uptime = StringToDate(sd,sb)   '<== formating the date/time value
    Exit For
   Next
 'Clear the memory from unnecessary values 
 Set objSYS = nothing
  Set objWMI = nothing
  Set sd = nothing
  Set sb = nothing
  Object.SetTimer 1000, 1000  
End Sub
Sub Object_OnTimer1000 '<== main timer
   desktopx.object("localtime").text = FormatDateTime(Time,3) '<== this will show you current time 
   desktopx.object("worktime").text = GetUptime(uptime,Now)
End Sub
Function GetUptime(t1,t2) '<== refreshing the value of uptime
   '''Dim''' xt,h,m,s
   xt = DateDiff("s",t1,t2)     '<== Date different between the uptime and current date/time
   h = int(xt/3600) Mod 100  '<== get the hours
   m = int(xt/60) Mod 60        '<== get the minutes
   s = xt Mod 60                     '<== get the seconds
   h = AddZero(h)
   m = AddZero(m)
   s = AddZero(s)
   GetUptime = h&":"&m&":"&s
End Function
Function AddZero(val) '<== add zero before the number
   If len(val) = 1 Then AddZero = "0"&val Else AddZero = val
End Function
Function StringToDate(xd,xb)
  If left(LCase(xd),1) = "d" Then '<== what is the first - the mohth or the day?
   StringToDate = cdate(mid(xb, 7, 2)&"."& _
   mid(xb, 5, 2)&"."&left(xb, 4) _
   &" "&mid (xb, 9, 2)&":"&mid(xb, 11, 2)&":"&mid(xb, 13, 2))
  Else
   StringToDate = cdate(mid(xb, 5, 2)&"/"& _
   mid(xb, 7, 2)&"/"&left(xb, 4) _
   &" "&mid (xb, 9, 2)&":"&mid(xb, 11, 2)&":"&mid(xb, 13, 2))  
  End If
  'As a result you will get the string like: "27.09.2007 10:56:14" if the day is first or "09/27/2007 10:56:14" in the other case.
End Function
Sub Object_OnScriptExit
   Object.KillTimer 1000 '<== Clear all...
   Set uptime = nothing 
End Sub


[edit] How to Get a List of Attributes from MP3 Files

Sometimes we need work with media files like audio, video, photo etc... For example when we create new Media Player. What the information we can find in the *.mp3 files and how to get it?

First of all you need create a simple listbox by using DesktopX Listbox ActiveX control to display the results and the Script Object to apply this code:

Sub Object_OnScriptEnter
  desktopx.ScriptObject("xbox").control.resetlist
End Sub
Sub Object_OnLButtonUp(x,y,dragged)
  If Not dragged Then
   On Error Resume Next
   Dim xfile,objFSO
    xfile = inputbox("Enter the path to *.mp3 file to get its attributes","Input Box")
   If xfile = "" Then Exit Sub
   Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(xfile) Then Call GetFileInfo(xfile)
   Set xfile = nothing
   Set objFSO = nothing
  End If
End Sub
Sub GetFileInfo(xfile)
  On Error Resume Next
  Dim objFSO,objFIL,fopath,finame,objShell,objFOL,objItem
   desktopx.ScriptObject("xbox").control.resetlist
  Set objFSO = CreateObject("Scripting.FileSystemObject")
   fopath = objFSO.GetParentFolderName(xfile)
  Set objFIL = objFSO.GetFile(xfile)
   finame = objFIL.name
  Set objFIL = nothing 
  Set objFSO = nothing
  Set objShell = CreateObject("Shell.Application")
  Set objFOL = objShell.NameSpace(fopath)
  Set objItem = objFOL.ParseName(finame)
   desktopx.ScriptObject("xbox").control.additem "File Name: "&objFOL.GetDetailsOf(objItem,0)
   desktopx.ScriptObject("xbox").control.additem "File Size: "&objFOL.GetDetailsOf(objItem,1)
   desktopx.ScriptObject("xbox").control.additem "File Type: "&objFOL.GetDetailsOf(objItem,2)
   'desktopx.ScriptObject("xbox").control.additem "Last Modified: "&objFOL.GetDetailsOf(objItem,3)
   'desktopx.ScriptObject("xbox").control.additem "Date Created: "&objFOL.GetDetailsOf(objItem,4)
   'desktopx.ScriptObject("xbox").control.additem "Date Accessed: "&objFOL.GetDetailsOf(objItem,5) 
   'desktopx.ScriptObject("xbox").control.additem "Attributes: "&objFOL.GetDetailsOf(objItem,6)
   'desktopx.ScriptObject("xbox").control.additem "Status: "&objFOL.GetDetailsOf(objItem,7)
   'desktopx.ScriptObject("xbox").control.additem "Owner: "&objFOL.GetDetailsOf(objItem,8)
   desktopx.ScriptObject("xbox").control.additem "Author: "&objFOL.GetDetailsOf(objItem,9)
   desktopx.ScriptObject("xbox").control.additem "Title: "&objFOL.GetDetailsOf(objItem,10)
   'desktopx.ScriptObject("xbox").control.additem "Subject: "&objFOL.GetDetailsOf(objItem,11)
   'desktopx.ScriptObject("xbox").control.additem "Category: "&objFOL.GetDetailsOf(objItem,12) 
   'desktopx.ScriptObject("xbox").control.additem "Pages: "&objFOL.GetDetailsOf(objItem,13) 
   'desktopx.ScriptObject("xbox").control.additem "Comments: "&objFOL.GetDetailsOf(objItem,14)
   'desktopx.ScriptObject("xbox").control.additem "Copyright: "&objFOL.GetDetailsOf(objItem,15)  
   desktopx.ScriptObject("xbox").control.additem "Artist: "&objFOL.GetDetailsOf(objItem,16) 
   desktopx.ScriptObject("xbox").control.additem "Album: "&objFOL.GetDetailsOf(objItem,17) 
   desktopx.ScriptObject("xbox").control.additem "Year: "&objFOL.GetDetailsOf(objItem,18)
   desktopx.ScriptObject("xbox").control.additem "Track Number: "&objFOL.GetDetailsOf(objItem,19)
   desktopx.ScriptObject("xbox").control.additem "Genre: "&objFOL.GetDetailsOf(objItem,20)
   desktopx.ScriptObject("xbox").control.additem "Duration: "&objFOL.GetDetailsOf(objItem,21)
   desktopx.ScriptObject("xbox").control.additem "Bit Rate: "&objFOL.GetDetailsOf(objItem,22) 
   desktopx.ScriptObject("xbox").control.additem "Protected: "&objFOL.GetDetailsOf(objItem,23)
   'desktopx.ScriptObject("xbox").control.additem "Camera Model: "&objFOL.GetDetailsOf(objItem,24)
   'desktopx.ScriptObject("xbox").control.additem "Date Picture Taken: "&objFOL.GetDetailsOf(objItem,25)
   'desktopx.ScriptObject("xbox").control.additem "Dimensions: "&objFOL.GetDetailsOf(objItem,26) 
   '27,28 and 31 - the "empty" keys
   'desktopx.ScriptObject("xbox").control.additem "Episode Name: "&objFOL.GetDetailsOf(objItem,29) 
   'desktopx.ScriptObject("xbox").control.additem "Program Description: "&objFOL.GetDetailsOf(objItem,30)  
   desktopx.ScriptObject("xbox").control.additem "Audio Sample Size: "&objFOL.GetDetailsOf(objItem,32) 
   desktopx.ScriptObject("xbox").control.additem "Audio Sample Rate: "&objFOL.GetDetailsOf(objItem,33)
   desktopx.ScriptObject("xbox").control.additem "Audio Channels: "&objFOL.GetDetailsOf(objItem,34)
   'desktopx.ScriptObject("xbox").control.additem "Company: "&objFOL.GetDetailsOf(objItem,35)
   'desktopx.ScriptObject("xbox").control.additem "Description: "&objFOL.GetDetailsOf(objItem,36) 
   'desktopx.ScriptObject("xbox").control.additem "File Version: "&objFOL.GetDetailsOf(objItem,37)
   'desktopx.ScriptObject("xbox").control.additem "Product Name: "&objFOL.GetDetailsOf(objItem,38)
   'desktopx.ScriptObject("xbox").control.additem "Product Version: "&objFOL.GetDetailsOf(objItem,39) 
  Set objFOL = nothing
  Set objItem = nothing
  Set objShell = nothing
End Sub


[edit] How to Get a List of Media Files in a Folder

You need create a simple listbox to display the results and the Script Object to apply this code:

Sub Object_OnScriptEnter
  desktopx.ScriptObject("xbox").control.resetlist
End Sub
Sub Object_OnLButtonUp(x,y,dragged)
  If Not dragged Then
   On Error Resume Next
   Dim xfolder,xarray
    xfolder = System.FolderDialog ("Select a Music Folder to list its content...","",0)
    xarray = ""
   If xfolder = "" Then Exit Sub
   Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(xfolder) Then xarray = OnBrowse_Ex(xfolder,sf)
   Set xfolder = nothing
   Set objFSO = nothing
    If xarray <> "" Then Call ViewResults(xarray)
  End If
End Sub
Sub ViewResults(xarray)
   desktopx.ScriptObject("xbox").control.resetlist
  For Each item In SortArray(xarray,"</>")
   If len(item) > 4 Then
    desktopx.ScriptObject("xbox").control.additem "File Name: "&split(item,"|")(0)
    desktopx.ScriptObject("xbox").control.additem "File Path: "&split(item,"|")(1)
    desktopx.ScriptObject("xbox").control.additem ""
   End If
  Next
End Sub
Function OnBrowse_Ex(fp,sf) '<== Using the Recursive Algorithm to get a list of all of the files in a folder
  On Error Resume Next
  Dim objFSO,objFOL,objFIL,EXT,objSUB
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFOL = objFSO.GetFolder(fp)
   If objFOL.Files.count > 0 Then
    Set objFIL = objFOL.Files
     For Each Item In objFIL
      EXT = LCase(objFSO.GetExtensionName(Item.path))
      '<Filtering by file extension>
      If IsMedia(EXT) Then  sf = sf&Item.name&"|"&Item.path&"</>" '<== the separator
     Next   
    Set objFIL = nothing
   End If
  Set objSUB = objFOL.SubFolders
   If objSUB.count > 0 Then '<== get all subfolders
    For Each subItem In objSUB
     Call OnBrowse_Ex(subItem.path,sf)
    Next
   End If    
  Set EXT = nothing 
  Set objSUB = nothing
  Set objFOL = nothing
  Set objFSO = nothing
   OnBrowse_Ex = sf
End Function
Function IsMedia(val)
   Select Case LCase(val) '<== audio, video, movie, images, metafiles, etc... >
    Case "aif","aifc","aiff","asf","asx","au","avi","cda","dvr-ms","ifo","jpeg","jpg","m1v","m3u","m3u8","mid","midi","mp2","mp3","mpa","mpe","mpeg","mpg","ogg","pls","rmi","snd","vob","wav","wax","wm","wma","wmd","wmv","wmx","wpl","wvx"
    IsMedia = True
   Case Else
    IsMedia = False
  End Select
End Function
Function SortArray(xarray,sep) '<== THE ALPHABETICAL SORTING 
     xarray = split(xarray,sep)
    For i = 0 To UBound(xarray) - 1
       j = i
     For k = i To UBound(xarray)
       If xarray(j) > xarray(k) Then
         j = k
       End If
     Next
      strtemp = xarray(j) '<== bubble sorting
      xarray(j) = xarray(i)
      xarray(i) = strtemp
    Next
     SortArray = xarray
End Function


This is all for now. Please let me know what the code examples you want see at the next time. I have more than 500 codes in my scripts library that can do nearly everything but I don't know what do you need.

Have a good time with DesktopX!