Scripting the Clipboard Contents in WSH

So you’re working with WSH and you’re wondering how you can get or set the contents of the Windows clipboard. Unfortunately, WSH does not have this ability natively and there are no COM objects or WMI methods for performing this action either, but you’ll be happy to see this workaround that will let you get and set the clipboard contents using Internet Explorer.

Quite often we find ourselves trying to perform an action or complete a task that is not native to either VBScript or WSH. In these cases, it’s helpful to use another program that has the abilities we’re looking for through its scripting interface. Typically this is done with Internet Explorer.

“Why Internet Explorer?” You ask.

Internet Explorer works very closely with the Windows Shell and therefore has a great deal of functionality that goes beyond simply grabbing and rendering web pages. Furthermore, it also has a very extensive scripting interface that makes the majority of these features available to scripters through its COM interface.

We’re going to harness some of that power now. Because Internet Explorer is designed to be scriptable, we’re able to launch the application and pass client-side browser script to the application as well. Let’s take a look at the code to retrieve the contents of the Windows clipboard.

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
strTemp = objIE.document.parentwindow.clipboardData.GetData("text")
objIE.Quit
WScript.Echo strTemp

In the first two lines, we create an instance of Internet Explorer and navigate it to a blank page. Once we have the application open, we’re able to perform a little workaround.

You’ll notice in the third line, we’re not using the WSH object model. Those objects and methods do not belong to the Internet Explorer object directly. They belong to Internet Explorer’s Document Object Model, but because Internet Explorer is designed to be scriptable, we’re able to pass them through the Internet Explorer object’s COM interface.

We then wrap up our simple example by closing Internet Explorer and displaying the string contents. Simple. However, there are a couple limitations to keep in mind.

First and foremost, the clipboard functionality in Internet Explorer is limited to text data only. You will not be able get or set binary data. So while you couldn’t retrieve an image on the clipboard, you could retrieve a path to one.

Second, this does require an instance of Internet Explorer. This provides a little overhead for your script, but it makes for a native workaround since Internet Explorer is present on nearly every Windows machine.

Now let’s take a look at how to copy information to the clipboard in WSH.

Placing text on the clipboard is just the opposite of taking it off. We’ll just use SetData instead of GetData.

strTemp = "Some clipboard text." & VbCrLf
strTemp = strTemp & "It can even contain linefeeds!"
 
Set objIE = WScript.CreateObject("InternetExplorer.Application")
objIE.Navigate "about:blank"
Do Until objIE.ReadyState = 4
	WScript.Sleep 100
Loop
 
objIE.document.ParentWindow.ClipboardData.SetData "text", strTemp
objIE.Quit

You’ll notice that the construct is the same. We first open an instance of Internet Explorer and then make use of it’s DOM to add the data to the clipboard. What you’ll notice different is the addition of the Do loop.

The Do loop acts as a sort of timer that waits to make sure that the application is fully initialized. This prevents any errors that might occur if you tried calling the SetData method before the DOM fully initialized. You could also apply this technique to the first example if you wanted to be thorough.

You now have a really nice workaround that will let you allow you to copy and paste data in the Windows clipboard through WSH. This can be a pretty handy workaround for temporarily saving information or for moving information between applications.

If you want to get or set binary data or if you don’t want to use Internet Explorer, there are many third-party ActiveX controls available. The downsides are that they would need to be installed on every machine that your script runs on and, to my knowledge, there aren’t any free ones available.

Tags

Like the read? Share it!

10 Comments

  • I Googled for something completely different, but found your page…and have to say thanks. nice read….

  • Thanks, Donna. I appreciate your kind words and support.

  • I’m having an issue making this code work on one of my machines. It works fine on my XP machine but locks up on my Vista machine at the SetData instruction. The script goes off into never-never land but the rest of my machine if free. My guess is it has something to do with security issues. Any ideas?

  • This could be a security issue. I’ll see if I can replicate the error and come up with a solution.

  • Thanks for the code snippet. If the people at Microsoft would just use their own software for 15 minutes they would see that a wscript get/set clipboard data fn. would be a commonly needed operation.

  • I agree completely. Unfortunately, since VBScript and WSH are no longer being actively developed it doesn’t appear we’ll be seeing that feature added.

  • There are some solutions for getting around the security issue:
    http://stackoverflow.com/questions/128463/use-clipboard-from-vbscript

  • Back in August of 2007 I wrote an entry about Scripting the Clipboard Contents in WSH in which I addressed a means of using Internet Explorer automation for setting and retrieving the […]

  • […] accomplish was accessing the clipboard from WSH. Digging the internet I found some solutions like this one based on Internet Explorer Automation. There are several problems with this approach as you can […]

  • Have you ever considered about adding a little bit more
    than just your articles? I mean, what you say is important and all.
    However think of if you added some great images or videos to
    give your posts more, “pop”! Your content is excellent but with images and clips, this website could certainly be one of the best in
    its field. Awesome blog!

Leave a Reply

Contact

Wanna say hello?
Drop us a line!

You'll find us here

1 Microsoft Way,
Redmond,
WA 98052, United States