Can I Copy Script Output to the Clipboard?


Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there any way to have my script output copied to the Clipboard?

— ZW, Marseilles, France

Spacer Hey, Scripting Guy! Answer Script Center

Hey, ZW. Well, as long as you don’t mind crazy work-arounds then, yes, it’s actually pretty easy to copy script output to the Clipboard. To start with, you need to construct a string that contains the desired output. After that, you create an instance of Internet Explorer, and open it to a blank page. You can then take advantage of the capabilities built into the Internet Explorer object model to copy the string to the Clipboard; in particular, you use the clipboardData.SetData method to accomplish this trick. A simple script to copy something to the Clipboard looks like this:

strCopy = "This text has been copied to the clipboard."
Set objIE = CreateObject("InternetExplorer.Application")
objIE.document.parentwindow.clipboardData.SetData "text", strCopy

Run the script, then open up Notepad and click Paste; you should see your string in all its glory.

By the way, all of this happens “under the covers;” Internet Explorer won’t actually pop up on your screen. That’s because, by default, any instance of IE you create from a script runs hidden, at least until you do something like this:

objIE.Visible = True

For more information about clipboardData.SetData, see the DHTML reference documentation on MSDN.



No Comments.