Downloading Data from the InternetMany Desktop Sidebar panels download data from the Internet and display it conveniently in the panel.
There are two ways to download data from the Internet in the script.
TODO: There is also a new RSS API in Desktop Sidebar which can be used by script panels.
1. DownloadFile - a Toplevel MethodThis method allows you to download a certain file from the Internet to any location on the user's hard drive.
The method's usage is as follows:
CODE
DownloadFile(filePath, url, routine, obj);
Parameters:- filePath (string) - specifies the local file path to which to file will be downloaded.
- url (string) - the Internet address from which the file will be downlaoded.
- routine (string) - the name of the method that will be called when the download is finished.
- obj (object) - any object, that will be passed as a parameter when calling routine when the download finishes.
Many times it is useful to specify in the filePath parameter a temporary file path (in the system Temp folder). To do this, one must first initialize a FileSystemObject and use its GetTempName method to get a temporary file path.
Note: this function cannot be used to download authenticated (password-protected) web pages. Instead, use the XmlHttp method that is described below.
Examples:This is an example that shows how to download a file to the user's c:\ drive. When the download is finished, a Message Box is shown. If there is an error downloading, a different Message Box is shown.
CODE
//Initiate the download process that will call DownloadFinished() in the end.
DownloadFile("c:\\newfile.html" ,"http://my.site.com/file.html", "DownloadFinished", 0)
function DownloadFinished(success, fileName, param)
{
//If the download succeeded show a success Message Box.
if(success)
{
sidebar.MsgBox("File downloaded to: " + fileName, "Success", 0);
}
else
{
//An error occurred while downloading the file.
sidebar.MsgBox("An error occurred.", "Download Failed", 0);
}
Following is an example of using DownloadFile to download the file to a temporary location and then open it. In the end, the temporary file is deleted.
CODE
//Initialize the FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
//Get a path for the downloaded file in the Temp folder.
var tempPath = fso.GetSpecialFolder(2).Path + "\\" + fso.GetTempName();
//Initiate the download process that will call downloadFinished() in the end.
DownloadFile(tempPath ,"http://my.site.com/file.html", "downloadFinished", 0)
function downloadFinished(success, fileName, param)
{
//If the download did not succeed show a Message Box and return.
if(!success)
{
sidebar.MsgBox("Error download file.", "Panel Error", 0);
return;
}
//The download succeeded, open the downloaded file and read its contents as text.
var textFile = fso.OpenTextFile(fileName, 1);
var text = textFile.ReadAll();
//Close the file object and delete it (we don't need it anymore).
textFile.Close();
fso.DeleteFile(fileName);
//Display a Message Box with the file's contents.
sidebar.MsgBox("Downloaded file contents:\n" + text, "Download Succeeded", 0);
}
2. XmlHttp - an ActiveX ObjectMicrosoft supplies an object to download data from the Internet via scripting as a part of Windows (note that some users may have to update their MSXML version with a simple download from the Microsoft website).
Like Desktop Sidebar's DownloadFile, this method too is asynchronus - meaning it doesn't block the application while it download the data, but instead calls a specific method when it is finished. The difference between the two is that this method downloads the file straight to the memory (into a variable) instead of into a file on the user's hard drive.
Example:The following example will try to download a file and display its contents. It will report in case of an error and stop the process.
CODE
var httpRequest = false;
function makeRequest(url)
{
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
if (!httpRequest)
{
sidebar.MsgBox("Cannot initialize XMLHTTP object. Please make sure you have the latest version of MSXML installed.", "Panel Error", 0);
return;
}
httpRequest.onReadyStateChange = stateChange;
httpRequest.open("GET", url, true);
httpRequest.send(null);
}
function stateChange()
{
if(httpRequest.readyState == 4)
{
if (httpRequest.status == 200)
{
sidebar.MsgBox("Downloaded text:\n" + httpRequest.responseText, "Success", 0);
} else {
sidebar.MsgBox("Error downloading data.", "Panel Error", 0);
}
}
}
makeRequest("http://my.site.com/file.html");
3. RSS Feeds APIDesktop Sidebar exposes a set of easy-to-use methods and objects to work with RSS feeds and data.
To-do.