A Simple Windows Desktop Application in VBScript

How to use the Ajax Technique with VBScript

A VBScript on a Windows Desktop - Mark Alexander Bain
A VBScript on a Windows Desktop - Mark Alexander Bain
If anyone thought that Ajax was limited to web pages, or even Javascript, then they's be wrong. This article shows how to obtain stock quotes without a web browser.

Every programmer involved with the Internet will be aware of Ajax (Asynchronous Javascript and XML), but they may not realize that the techniques involved are not limited to Javascript alone - the techniques can also be used, for example, with VBScript; and this raises an interesting possibility - the ability to use the Ajax technique to create a Windows Desktop application.

All that's needed for the Ajax technique to work with VBScript is:

  • VBscript to be installed (but fortunately that comes as standard on every Windows pc)
  • the XML HTML request object to be installed (again that's built into every Windows computer)
  • a server script (on an web or network server)
  • a VBScript that will send data to the server and then process the data returned by it

In this example:

  • a PHP script on the server will take a company acronym as an input and return the current stock quote from Yahoo! Finance
  • the VBScript code will call the PHP script on the server and then display the result

The first step, therefore, is to set up the PHP script on the server.

Setting up the Server Side of the Application

In the final application the VBScript will call a PHP script and, as with any code, that script should (wherever possible):

  • consist of a set of functions allowing the code to be reused as required
  • store the functions in libraries - again allowing the code to be reused

And the PHP library for this application will look like:

<?php
function get_stock_quote ($company_symbol) {
/* Format the url to be sent to the server */
$url = "http://quote.yahoo.com/d/quotes.csv?"
. "s=" . $company_symbol . "&f=l1&e=.csv";
/* Set the expected number of bytes to be returned */
$filesize = 2000;
/* Open the data stream */
$handle = fopen($url, "r");
/* Read the data */
$quote = fread($handle, $filesize);
/*Close the data stream */
fclose($handle);
/* Output the result */
echo $quote;
}
?>

If the script has been saved in a library (named, for example, yahoo_library.php) then it can be called from a PHP script (and it's this PHP script that will be called from VBScript):

<?php
/* Load the library containing the function */
include ("yahoo_library.php");
/* Obtain the company symbol from the variables sent to the script */
$company_symbol = $_REQUEST["company_symbol"];
/* Pass the company symbol to the function */
get_stock_quote ($company_symbol);
?>

Testing the Server Side of the Application

Before going any further it's a well to test the PHP script - and all that's needed for that is to enter the correct url into a web browser; and that url must contain a valid Yahoo! Finance company acronym; so, if the PHP script has been saved as yahoo_stock.php then the url will be something like:

http://<my server>/yahoo_stock.php?company_symbol=NOVL

The resulting web page will display the current stock quote for Novell Inc.

The Yahoo! Finance VBScript Client Application

All of the hard work has now been done (mainly by Yahoo! Finance) and so all that's left to do is to write a simple piece of VBScript that will:

  • accept an input from a user
  • format a url and sent it to the server
  • show the user the results

This can easily be accomplished in a few lines of code:

' The script will accept an input from either
' * the command line
' * an input box
if WScript.Arguments.Count <> 1 then
company_symbol = inputbox ("Please enter a company achronym")
else
'The company symbol is the first (and only) input variable
company_symbol = WScript.Arguments.Item(0)
end if
' Format the url
url = "http://192.168.1.3/yahoo_stock.php?" _
& "company_symbol=" & company_symbol
' Create the XML HTTP object
Set xmlhttp = CreateObject("microsoft.xmlhttp")
' Send the url to the server
xmlhttp.open "get", url, false
xmlhttp.send ""
' Output the result
if WScript.Arguments.Count <> 1 then
msgbox(xmlhttp.responseText)
else
WScript.Echo xmlhttp.responseText
end if
' Finish by cleaning up any objects created
set xmlhttp = nothing

Running the Yahoo! Finance VBScript Client Application

If the VBScript is saved as yahoo.vbs then it can be run in either of two ways:

  • open up Windows Explorer and double click on the yahoo.vbs icon
  • open the MS-DOS Prompt and use cscrip to run yahoo.vbs, for example:
    cscript /nologo yahoo.vbs rht

Conclusion

This simple example shows that the Ajax technique is not the preserve of web sites, and that the same on-line sources can easily be used to create Windows Desktop applications.

Further Reading

Using Yahoo! Financial Stock Quotes

Currency Conversion with Yahoo! Finance

Accessing Yahoo! Finance from PHP

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement