RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Apply the New Vista APIs to Sidebar Gadgets, Part 2 : Page 3

Explore the various Windows Vista Sidebar gadget objects and how they can be used to enhance the functionality of your gadget.

The System.Shell.Folder object enables you to perform file operation such as copying and moving files and creating new folders. It exposes a collection of System.Shell.Item objects.

Here's an example that shows you how to copy files from C:\folder1 into C:\folder2 using the copyHere() method:

   var destFolder = System.Shell.itemFromPath("C:\\folder2");
   var sourceFolder = System.Shell.itemFromPath("C:\\folder1");

   //---copies all the files in folder1 into folder2---
   for (var i = 0; i<sourceFolder.SHFolder.Items.count; i++ ){
      var item = sourceFolder.SHFolder.Items.item(i);
      destFolder.SHFolder.copyHere( item, 16 + 256 + 512 );
The second parameter of the copyHere() method takes an optional flag argument, which can be a combination of the following values:




Do not display a progress dialog box.


Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.


Respond with "Yes to All" for any dialog box that is displayed.


Preserve undo information, if possible.


Perform the operation on files only if a wildcard file name (*.*) is specified.


Display a progress dialog box but do not show the file names.


Do not confirm the creation of a new directory if the operation requires one to be created.


Do not display a user interface if an error occurs.


Only operate in the local directory. Don't operate recursively into subdirectories.


Do not copy connected files as a group. Copy only the specified files.

To move files instead of copying them, use the moveFile() method, like in the following example, which moves all the files in folder1 to folder2:

   var destFolder = System.Shell.itemFromPath("C:\\folder2");
   var sourceFolder = System.Shell.itemFromPath("C:\\folder1");

   //---moves all the files in folder1 into folder2---
   while (sourceFolder.SHFolder.Items.count>0) {
      var item = sourceFolder.SHFolder.Items.item(0);
      destFolder.SHFolder.moveHere( item, 16 + 256 + 512 );
You can also create a new folder; here's a code snippet that does so in C:\folder1:

   var sourceFolder = System.Shell.itemFromPath("C:\\folder1");
   sourceFolder.SHFolder.newFolder("New Documents",0);
The following code displays the number of items contained within the C:\folder1 folder:

   var sourceFolder = System.Shell.itemFromPath("C:\\folder1");
   var result = document.getElementById("result");

   //---get info about itself---
   var folder = sourceFolder.SHFolder.Self;

   //--displays how many items in the folder---
   result.innerHTML = folder.SHFolder.Items.count;

Author's Note: The Parent property is not working in the current version of the Sidebar.

The System.Shell.Item object returns detailed information about an item in the shell (such as files, folders, and shortcuts).

The code in Listing 4 shows you how to display the attributes of each file located in folder1.

The invokeVerb() method performs a specified action on a file. For example, the verb Open opens a file with its associated program. The following code first checks to see if an item's name ends with the .txt extension and opens it if it does (Notepad launches by default):

      if (item.name.indexOf(".txt") != -1) {
Some commonly used verbs are Open, Edit, New, Print, and Show. You can find out which verbs are applicable for a file type by right-clicking on a file type in Windows Explorer and then viewing its list of applicable actions (see Figure 1).

Figure 1. View the Verbs: Find which verbs are applicable for a file type by right-clicking on a file type in Windows Explorer.
Figure 2. Metadata: The keys are listed in the Description section of the Details tab.

The metadata() method returns the metadata of an item based on the key (such as Title, Subject, Tags, Categories, Comments, and so on) specified. For example, the following code shows the Title metadata for a PowerPoint file:

      if (item.name.indexOf(".pptx") != -1) {
         var metaData = item.metadata("Title");
         result.innerHTML += metaData;
You can obtain the list of keys for a particular file type by right-clicking a file and selecting Properties. On the Details tab, the keys are listed in the Description section (see Figure 2). Common metadata keys are Title, Subject, Tags, Categories, and Comments.

The System.Shell.RecycleBin object enables you to programmatically manipulate the Recycle Bin on your computer.

Here's a code example that shows you how to display the number of files and folders in the Recycle Bin, as well as the current size of the Recycle Bin:

         var result = document.getElementById("result");
         result.innerHTML = "fileCount: " +
            System.Shell.RecycleBin.fileCount + "<br/>" +
            "folderCount: " +
            System.Shell.RecycleBin.folderCount + "<br/>" +
            "sizeUsed: " +
            System.Shell.RecycleBin.sizeUsed + "KB";

Figure 3. The Recycle Bin Properties Dialog: The inline code example displays the Recycle Bin Properties dialog.

The following code example first displays the Recycle Bin Properties dialog (see Figure 3), followed by deleting an item (by moving it into the Recycle Bin). Finally, it asks the user if he wants to empty the Recycle Bin:

         //---shows the recycle bin status---

         //---delete a particular file (move to recycle bin)---

         //---empties the recycle bin---
The System.Shell.RecycleBin object has one event, onRecycleBinChanged, which allows script commands within a gadget to be run when the contents of the Recycle Bin are modified, such as when files, folders, or links being added or deleted from the set of items currently stored in the Recycle Bin.

The onRecycleBinChanged event fires whenever the content in the Recycle Bin is modified. The following code shows you how to wire an event handler to handle the onRecycleBinChanged event:

      System.Shell.RecycleBin.onRecycleBinChanged = RecycleBinChanged;

      function RecycleBinChanged()
         var result = document.getElementById("result");
         result.innerHTML = "Recycle Bin Changed";
The System.Shell.Drive object enables you to obtain detailed information about a drive.

The code in Listing 5 shows you how you to retrieve detailed information about the C:\ drive.

The driveType property returns one of the following values:




The specified drive is invalid.


The drive is a floppy drive.


The drive is a removable drive, such as a USB or cartridge drive.


The drive is a fixed drive.


The drive is a remote (network) drive.


The drive is a CD-ROM drive.


The drive is a RAM disk.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date