How Can I Grab a URL From the Clipboard and Then Open That Web Site in a Browser?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I grab a URL from the clipboard and then open that Web site in a browser?

— CL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CL. Interesting question, or, should we say, questions. That’s because there’s really two issues here. The first one is easy: can I use a script to open a specified Web site? As you probably know, the answer to that one is a resounding yes. Here’s a sample script that stores the Script Center’s URL in a variable named strURL. The script then creates an instance of the WSH Shell object, and uses the Run method to open up your default Web browser and navigate to the specified URL:

strURL = “http://www.microsoft.com/technet/scriptcenter/default.mspx”
Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(strURL)

The second question is a bit trickier: can I grab information off the clipboard using a script? It turns out that the answer to this one is also yes, although you have to get at the clipboard through the backdoor.

Neither WSH nor VBScript are able to interact with the clipboard: neither one allows you to copy data to the clipboard and or to paste data from the clipboard. Internet Explorer, on the other hand, can interact with the clipboard. (Hey, Internet Explorer can do anything!) So, we’ll just let IE do the work for us. If you want to grab data from the clipboard, you can use code similar to this:

Set objIE = CreateObject(“InternetExplorer.Application”)
objIE.Navigate(“about:blank”)
strURL = objIE.document.parentwindow.clipboardData.GetData(“text”)
objIE.Quit
Wscript.Echo strURL

What we’re doing here is creating an instance of Internet Explorer and opening it to a blank page. Note that you won’t actually see this instance of IE; that’s because we didn’t set the Visible property to TRUE. Instead, everything happens in the background.

We then use the clipboardData.GetData method to get the text that happens to be sitting on the clipboard and store it in the variable strURL; that’s what this line of code does:

strURL = objIE.document.parentwindow.clipboardData.GetData(“text”)

We dismiss this instance of IE (objIE.Quit) and then echo the value that we retrieved from the clipboard.

Give this a try: copy some text to the clipboard and then run the script. You should get back a message box consisting of that text you just copied to the clipboard.

Now it’s just a matter of putting the two script halves together to make a whole. Here’s a script that grabs a URL from the clipboard and then opens that Web site in your default Web browser:

Set objIE = CreateObject(“InternetExplorer.Application”)
objIE.Navigate(“about:blank”)
strURL = objIE.document.parentwindow.clipboardData.GetData(“text”)
objIE.Quit

Set objShell = CreateObject(“Wscript.Shell”) objShell.Run(strURL)

So maybe that wasn’t so bad after all. And as an added bonus, this script doesn’t limit you to just opening Web sites. Suppose you have a file path like C:\Scripts\ScriptLog.txt on the clipboard. Run the script, and the file will open up in Notepad (or whatever application you have associated with .txt files). If you have the path to a .doc file on the clipboard, the script will open that document in Microsoft Word. It’s really more of a general purpose file opening script than it is a Web site-only opening script.

0 comments

Discussion is closed.

Feedback usabilla icon