Starting a browser

Simon Willison writes about starting a browser from the Windows Run box: “I can hit CTRL+R, type one of those browser names, paste in a URL and hit enter to instantly load that page”. Me, I do it a more efficient way :-) This is the code for startbrowser.vbs, a Windows Scripting Host script that gives you a menu of browsers from which to choose when you start it, as long as you’ve got a URL on the clipboard. (If you have no URL on the clipboard, it does nothing.) Stick a shortcut to it in your Start Menu and bind a key combination to it (view the properties of the shortcut and set a “Shortcut key” — I use B, which means that I hit ctrl+alt+B and it starts the code) — if you have a URL on the clipboard then it shows you the window and gives you ten seconds to press a key. Note that you must edit the “browsers” line in the script to specify your browsers and locations of same. This script probably requires VBScript 5.5 and IE 5.5 (but if you’re looking to test on multiple browsers you’ll almost certainly have IE).

' startBrowser.vbs
' If you have a URL on the clipboard, offers you a menu
' of browsers and then starts your choice with the one you want.

' Uses code from HTMLClipboard.vbs by GBorn (www.borncity.de)
'

Dim oIE
Set oIE = WScript.CreateObject("InternetExplorer.Application")

' Set your browsers variable: 
'   pipe (|) separates browsers
'   a browser is exe-with-full-path,name,accesskey
browsers = "d:mozilla1.0mozilla,Mozilla,m|iexplore,Internet Explorer,e"


clipboard = getClipBoardText()
' Does it look like a URL?
if left(clipboard,7) = "http://" then
    html = "<html><head><title>Choose your browser to open url " & clipboard & "</title><script>document.onkeypress=kp;" & vbCrLf & _
        "function kp() { switch(String.fromCharCode(window.event.keyCode)) {"
    browserArray=split(browsers,"|")
    for b=0 to ubound(browserArray)
        brArray=split(browserArray(b),",")
        html = html & "case '" & brArray(2) & "': document.getElementById('browser').value='" & brArray(1) & "'; break; " & vbCrLf
    next
    html=html & "} }</script></head>" & _
        "<body bgcolor='silver'><h1 style='font-size: 12px'>Choose your browser to open url " & clipboard & "</h1>"
    for b=0 to ubound(browserArray)
        brArray=split(browserArray(b),",")
        html = html & brArray(1) & " (<strong>" & brArray(2) & "</strong>)<br>"
    next
        html=html & "<input type='hidden' name='browser' id='browser' value=''></body></html>"
    oIE.visible = 1
    MakeIEDoc html
    ' Now poll IE to see if they've picked a browser or 10 seconds have passed
    closetime=10000     ' thousandths of a second
    pollinterval=500    ' thousandths of a second
    count=0
    do
        count = count + pollinterval
        if count >= closetime then exit do
        browsername = oIE.Document.All.browser.Value
        if browsername <> "" then exit do
        wscript.sleep pollinterval
    loop
    if browsername <> "" then
        ' Find their browser and start it
        for b=0 to ubound(browserArray)
            brArray=split(browserArray(b),",")
            if browsername = brArray(1) then
                set oShell = createobject("WScript.Shell")
                oShell.run(brArray(0) & " " & clipboard)
            end if
        next
    end if
end if
oIE.Quit
wscript.quit



'###################
' Helper procedures
'###################
function getClipBoardText()
Dim txt
MakeIEDoc "<html><head><title>Clipboard Exchange Helper</title></head>" & _
       "<body bgcolor='silver'>" & _
       "<textarea name='exch' rows='8' cols='80'></textarea>" & _
       "</body></html>"

txt = "foo"
oIE.Document.All.exch.Value = txt
oIE.Document.All.exch.select()
oIE.Document.execCommand("Paste")
getClipBoardText = oIE.Document.All.exch.Value
end function

Sub MakeIEDoc (html)
' Launch Internet Explorer & prepare a page with a text box
' define HTML code with a text area

' *** launch Internet Explorer ***
  oIE.left=50             ' window position
  oIE.top = 100           ' and other properties
  oIE.height = 200
  oIE.width = 580
  oIE.menubar = 0         ' no menu
  oIE.toolbar = 0
  oIE.statusbar = 0
  oIE.navigate "about:" & html ' Helper window
' we keep the browser window invisible! Uncomment the
' next line, if you like to view the browser window for tests
'  oIE.visible = 1         ' keep visible

  Do While (oIE.Busy):Loop ' Important: wait till MSIE is ready
End Sub
'* End

More in the discussion (powered by webmentions)

  • (no mentions, yet.)