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 G�Born (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:\mozilla\1.0\mozilla,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
this looks promising and kind of what i was looking for. is there a way to modify this to become the default script to be called when a program or .url file trys to launch a browser ? i switch between firefox and ie too frequently to keep switching the default behavior, and not all windows let you copy and paste links
141 weeks later
hate to double post, but coincidentally i found you thru a google search, and like your site layout.
… also i doubt you even remember something you posted ~3yr ago but this script is generating errors when it trys to run. its complaing about the comment character on line 1 , even though i have other scripts that also have comments using same character on line 1.
[ also you have a pretty slick site nav and even this text box is well done, i may find some of this code making its way into my pages if you dont mind] [[any way to edit prior posts ? ]]
141 weeks later
PidGin128: On Windows I think you’ll need to change the association for .html files, but I’m not sure. I have no problem with other people using bits of code. You can’t edit old posts because I have no way of telling that the you trying to edit the post is the you who posted it, and I don’t want to require login for posts.
141 weeks later
Hy there. This script gives me the next error
Object doesnt support this property or method: oIE.Document.All.exch.Value = txt
233 weeks later
Alex: may be an IE7 problem? I don’t use this script any more, I’m afraid…
233 weeks later
I hate spammers! (
240 weeks later