Build Yahoo! searches into Windows programs or ASP pages with VBScript.
VBScript is a general-purpose scripting language for Windows, and it gets its name from Visual Basic, its big brother of a programming language. With a few tweaks here and there, the code in this hack could add Yahoo! searching to Office applications or an ASP-powered web page. This hack is written to run as a Microsoft Windows Script and it provides just the basics for building a Yahoo! Search query and presenting the results.
Microsoft Windows Script is built into the fabric of the Windows operating system and is used primarily by system administrators to automate some tasks involved with—you guessed it—system administration. But Microsoft Windows Scripts can also be used to automate applications and send data back and forth between programs.
If your Windows installation is up-to-date, you shouldn’t need to install anything extra to run this hack. But if it’s been a while since you’ve run Windows Update, you might want to grab the latest version of Microsoft Windows Script at http://www.microsoft.com/scripting. From that page, click Downloads and choose Microsoft Windows Script 5.6 or later for your version of Windows.
This hack also relies on the Microsoft XML Parser to sort the results from Yahoo!. Your system should already have a version of the parser installed, but if you run into trouble, you can always download the latest version at http://msdn.microsoft.com/xml. Once you’re there, click XML Downloads and choose the latest XML Core Services package you can find.
Tip
As always, be sure to grab a unique Yahoo! application ID for this script at http://api.search.yahoo.com/webservices/register_application.
Like any other script, the code is simply plain text in a standard text file. You could even use Notepad to save the following code to a file called yahoo_search.vbs:
' yahoo_search.vbs ' Accepts a search term and shows the top results. ' Usage: cscript yahoo_search.vbs <Query> //I ' ' You can create an AppID, and read the full documentation ' for Yahoo! Web Services at http://developer.yahoo.net/ 'Set your unique Yahoo! Application ID Const APP_ID = "insert your app ID" 'Grab the incoming search query or ask for one If WScript.Arguments.Length = 0 Then strQuery = InputBox("Enter a search Term") Else strQuery = WScript.Arguments(0) End If 'Construct a Yahoo! Search Query strLanguage = "en" strReqURL = "http://api.search.yahoo.com/" & _ "WebSearchService/V1/webSearch?" & _ "appid=" & APP_ID & _ "&query=" & strQuery & _ "&language=" & strLanguage 'Start the XML Parser Set MSXML = CreateObject("MSXML.DOMDocument") 'Set the XML Parser options MSXML.Async = False 'Make the Request strResponse = MSXML.Load(strReqURL) 'Make sure the request loaded If (strResponse) Then 'Load the results Set Results = MSXML.SelectNodes("//Result") 'Loop through the results For x = 0 to Results.length - 1 strTitle = Results(x).SelectSingleNode("Title").text strSummary = Results(x).SelectSingleNode("Summary").text strURL = Results(x).SelectSingleNode("Url").text strOut = (x + 1) & ". " & _ strTitle & vbCrLf & _ strSummary & vbCrLf & _ strURL & vbCrLf & vbCrLf WScript.Echo strOut Next 'Unload the results Set Results = Nothing End If 'Unload the XML Parser Set MSXML = Nothing
This code accepts a query on the command line when the script runs, or it asks the user for a query with the InputBox()
function. From there, the script uses the query to build the proper Yahoo! Web Search request. The results from the request are passed through the Microsoft XML Parser and formatted for display. WScript.Echo
sends the results to the user.
There are a couple of different ways to run the code. The most useful way to see the results is to run the script from a command prompt in interactive mode. Open a command prompt and type the following command:
cscript yahoo_search.vbs insert word
//I
Be sure to include multiword searches in quotes:
cscript yahoo_search.vbs "insert multiword phrase
" //I
The //I switch tells Microsoft Windows Script to output any results to the command line. Figure 4-6 shows this in action for the search term
vbscript
.
If you want to, though, you can just double-click the yahoo_search.vbs file as you would any other program. You’ll be prompted for a search word, and the results will be shown one at a time—all 10 of them—in window prompts like the one shown in Figure 4-7.
While this isn’t the handiest way to view search results, it shows that adding Yahoo! data to VBScript applications can be accomplished fairly quickly.
Get Yahoo! Hacks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.