Q:
Hi, scripting guy! How do I capture a URL from the clipboard and open the web site in a browser?
-- Cl
A:
Hello, cl. This is an interesting question, or we should say that it is two very interesting questions. Because you actually asked two questions. The first question is simple: Can I use a script to open a specific web site? You probably already know the answer. I can answer you out loud! The following is an example script that stores the URL of the script center in a variable named strurl. Then, this script creates an instance of the wsh shell object and uses the run method to open the default web browser and navigate to the specified URL:
CopyCode The Code is as follows: strurl = "http://www.microsoft.com/technet/scriptcenter/default.mspx"
Set objshell = Createobject ("wscript. Shell ")
Objshell. Run (strurl)
The second problem is tricky: Can I use a script to capture information from the clipboard? The answer to this question is "yes", although you must use a backdoorProgramTo the clipboard.
Neither wsh nor VBScript can interact with the clipboard: neither of them allows you to copy or paste data from the clipboard. On the other hand, Internet Explorer can interact with the clipboard. (Look, Internet Explorer is omnipotent !) So let ie do this for us. If you want to capture data from the clipboard, you can use code similar to the following code:
Set objie = Createobject ("internetexplorer. application ")
Objie. navigate ("about: blank ")
Strurl = objie.doc ument. parentwindow. clipboardData. getdata ("text ")
Objie. Quit
Wscript. Echo strurl
What we do here is to create an Internet Explorer instance and open it on a blank page. Note that you cannot actually see this ie instance, because the visible attribute is not set to true. All things happen in the background.
Then, we use the clipboardData. getdata method to obtain the text placed on the clipboard and store it in the variable strurl. This is what the following line of code does:
Strurl = objie.doc ument. parentwindow. clipboardData. getdata ("text ")
We disable this ie instance (objie. Quit) and then echo the value we retrieved from the clipboard.
Try to copy some text to the clipboard and then run the script. You should get a message box containing the text you copied to the clipboard.
Now there is one thing to do: combine these two half scripts to form a complete script. The following script captures a URL from the clipboard and then opens the website in the default web browser:
Set objie = Createobject ("internetexplorer. application ")
Objie. navigate ("about: blank ")
Strurl = objie.doc ument. parentwindow. clipboardData. getdata ("text ")
Objie. Quit
Set objshell = Createobject ("wscript. Shell ")
Objshell. Run (strurl)
This script is not good enough. It also has an advantage-it is not only used to open web sites. Assume that your clipboard has a file path, such as "C: \ scripts \ scriptlog.txt ". Run this script to open the file in Notepad (or any application you set to be associated with the. txt file. If there is a path to the. DOC file on your clipboard, this script will open this document in Microsoft Word. It is actually a common file opening script, not just an open script that can only be used for web sites.