Monday, February 8, 2010

Create a Win7 Gadget

This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista.  Gadgets can be dropped anywhere on the desktop to provide instant information, or to act as a gateway to a website or a larger program.  

What I like about gadgets is that they are really just small HTML pages.  That means:  No big-project development overhead.  If you have Notepad.exe and some knowledge about HTML, then you can write your own gadget.  Like HTAs, they run with with full local-program privileges, and can use ActiveX objects.  There is no limit to the creative things you can do with gadgets.

Some gadgets on the desktop
205192


How to Create a Gadget (the short version):
Here's all you need to do to create your own gadget:

1) Write an HTML page, say gadgetName.html

2) Write a short XML file, named (exactlygadget.xml

3) Copy these two files into a particular directory:
   userDir \ AppData\Local\Microsoft\Windows Sidebar\Gadgets\ gadgetName.gadget

The Details:
Follow these steps and you'll have a new gadget in a few minutes:


1
Create a new folder on your desktop and name itHelloGadget.gadget


2
Open that folder and create two files in that folder.  Here's the XML file (called a manifest):gadget.xml
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:

HelloGadget
1.0.0.0
Hello World Gadget.



Full




About the only place you might go wrong is misspelling the name of the HTML file in the    "HelloGadget.html" />line.  Here's that HTML file:HelloGadget.html
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:







Hello World!


3
Open a Windows Explorer and drill-down to locate thefollowing folder:   userDir \ AppData\Local\Microsoft\Windows Sidebar\Gadgets\...where userDir is your Windows User Name (the folder is in the "Desktop" virtual folder, just above "Computer" ) If you want, you can copy the following text and paste it into the address bar in any Explorer window (or in the Start/Search or Start/Run input boxes):
1:
2:
%localappdata%\Microsoft\Windows Sidebar\Gadgets
Looking in that folder, you will see the other gadgets that are already installed.  Each is in a folder that has a file extension of .gadget  A good way to learn about gadget programming is to examine the source files of the gadgets you can download from the Microsoft site.



4
Drag the HelloGadget.gadget folder from the desktopand drop it into that Gadgets directory.
All that's needed:  Two files in the right folder
205193


5
Right-click the desktop and select Gadgets. Your gadget is now visible in the gallery.
The Gadget Gallery
205194
It has the default icon, but the name is as was set in the XML file.  Drag it and drop it onto your desktop.


There you have it:  Your first gadget.  About all you can do with it is drag it around and close it.  But that's a start.  You can open the HTML file with your favorite text editor, make changes, and then drag it from the gallery to the desktop to see the effects of your changes.

Debugging
Alas, the usually-dependable all-purpose JavaScript debugging tool --  the alert() function -- is not available when running as a gadget.  One option is to drop the HTML file onto a browser and run it there -- where alert() will work.

With Windows 7, there is a new registry key that affects debugging.  Setting...

   [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Sidebar]
      "ShowScriptErrors"=dword:00000001

...will enable the display of script errors.  It's better than sitting there looking at the screen blankly and wondering why nothing is happening.

If you have Visual Studio, you can use its Just-In-Time debugging.  Just insert:
1:
2:
debugger;

...at a good starting point in the

No comments:

Post a Comment