(view source code of gconv.vbs as plain text)
Option Explicit
Dim i, strQuery
With WScript.Arguments
If .Named.Count > 0 Then Syntax
If .Unnamed.Count < 4 Then Syntax
For i = 0 To .Unnamed.Count - 1
strQuery = strQuery & "+" & .Unnamed(i)
Next
End With
strQuery = Mid( strQuery, 2 )
WScript.Echo GoogleUnitConversion( strQuery )
Function GoogleUnitConversion( myQuery )
' Name: GoogleUnitConversion
' Function: Convert units using Google Calculater and Internet Explorer
' Usage: WScript.Echo GoogleUnitConversion( "10+cm+in+inches" )
' Returns: 10 centimeters = 3.93700787 inches
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
Dim blnTimedOut, i, j, objIE, objMatches, objRE, strAllText, strMatch, strPattern, strURL
' Open the appropriate URL in Internet Explorer
strURL = "http://www.google.com/search?q=" & myQuery & "&pws=0&hl=en&num=1"
strPattern = "\)\s*" & Left( myQuery, InStr( myQuery, "+" ) -1 ) & "\s*[^=]+\s*=\s*[\d\., ]+(\s+[a-z]+)*\s*" _
& Right( myQuery, Len( myQuery ) - InStrRev( myQuery, "+" ) )
Set objIE = CreateObject( "InternetExplorer.Application" )
objIE.Visible = False
objIE.Navigate2 strURL
' Wait till IE is ready
i = 0
blnTimedOut = False
Do While objIE.Busy
WScript.Sleep 100
i = i + 1
' Time out after 10 seconds
If i > 100 Then
blnTimedOut = True
Exit Do
End If
Loop
' Retrieve the URL's text
If Not blnTimedOut Then strAllText = objIE.Document.Body.InnerText
' Close the Internet Explorer session
objIE.Quit
Set objIE = Nothing
' Use a regular expression to extract the result from the web page
Set objRE = New RegExp
objRE.Global = True
objRE.IgnoreCase = True
objRE.Pattern = strPattern
Set objMatches = objRE.Execute( strAllText )
j = objMatches.Count - 1
If j < 0 Then
strMatch = ""
Else
strMatch = Trim( Mid( objMatches.Item(j).Value, 2 ) )
End If
Set objMatches = Nothing
Set objRE = Nothing
GoogleUnitConversion = strMatch
End Function
Sub Syntax( )
Dim strMsg
strMsg = "GConv.vbs, Version 1.10" & vbCrLf _
& "Convert units using Google Calculater and Internet Explorer" _
& vbCrLf & vbCrLf _
& "Usage: CSCRIPT //NoLogo GCONV.VBS number unit IN new_unit" _
& vbCrLf & vbCrLf _
& "E.g: 10 cm in inches" _
& vbCrLf & vbCrLf _
& "Returns: 10 centimeters = 3.93700787 inches" _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" & vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit 1
End Sub
page last modified: 2024-04-16; loaded in 0.0092 seconds