Use the VBS implementation to crawl a URL from the Clipboard and then open the Web site in the browser _vbs

Source: Internet
Author: User
Tags blank page
Ask:
Hey, scripting guy!. How do I crawl a URL from the Clipboard and open the Web site in a browser?

--CL

For:
Hello, CL. This is an interesting question, or we should say that this 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 loudly, can! Here is a sample script that stores the URL of the Script Center in a variable named strURL. The script then 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:

Copy Code code as follows:

strURL = "Http://www.microsoft.com/technet/scriptcenter/default.mspx"
Set Objshell = CreateObject ("Wscript.Shell")
Objshell.run (strURL)


The second question is tricky: can I use a script to grab information from the clipboard? The answer to this question is "yes", although you must reach the Clipboard through the backdoor.

Neither WSH nor VBScript can interact with the Clipboard: Neither of them allows you to copy data to the Clipboard or paste data from the Clipboard. Internet Explorer, on the other hand, can interact with the Clipboard. (See, Internet Explorer is really omnipotent!) So, let IE do the work for us. If you want to crawl data from the Clipboard, you can use code similar to the following:

Set objIE = CreateObject ("Internetexplorer.application")
Objie.navigate ("About:blank")
strURL = ObjIE.document.parentwindow.clipboardData.GetData ("text")
Objie.quit
WScript.Echo strURL

Here's what we do: Create an instance of Internet Explorer and open it on a blank page. Note that you do not actually see this IE instance, because we did not set the Visible property to TRUE. All things happen in the background.

We then use the Clipboarddata.getdata method to get the text placed on the Clipboard and store it in the variable strurl, which is what the following line of code does:

strURL = ObjIE.document.parentwindow.clipboardData.GetData ("text")

We close this IE instance (objie.quit) and echo back the values we retrieved from the Clipboard.

Try the following: copy some text to the Clipboard, and then run the script. You should get a message box that contains the text that you just copied to the Clipboard.

There's only one thing left to do now: combine the two halves of the script together to form a complete script. The following script can crawl a URL from the Clipboard and open the Web site in the 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)

This script isn't bad. It also has the advantage that it is not only used to open a Web site. Suppose you have a file path on your clipboard, such as "C:\Scripts\ScriptLog.txt." Run this script, the file will open in Notepad (or any application that you set to be associated with a. txt file). If you have a path to a. doc file on your clipboard, the script opens the document in Microsoft Word. It's actually a generic file-opening script, not just an open script that can be used only for WEB sites.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.