Can I Copy Script Output to the Clipboard?

Can I Copy Script Output to the Clipboard?

  • Comments 7
  • Likes

Hey, Scripting Guy! Question

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

-- ZW, Marseilles, France

SpacerHey, Scripting Guy! AnswerScript 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.Navigate("about:blank")
objIE.document.parentwindow.clipboardData.SetData "text", strCopy
objIE.Quit

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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hello, Script Guy!

    I want to send my highlighted lines to the ClipBoard.

    I want to do it as one of my steps inside a VBS script.

    I cannot  use the common Ctrl+C.

    -----

    Is it at all possible ?

    Manfred Lutz.

    *******************

  • Is there a way to do this without IE?

  • @T Luke you can use the clip.exe program that is part of the operating system. In Windows PowerShell it is as simple as this:

    "Copy this string to the clipboard" | clip

    Using VBScript, you will need to create the wshshell object and use the exec method to start clip, and then play around with stdin to get your input. But it can be done ... Nowdays, attempting to automate IE is a hit or miss proposition due to all the security changes -- this particular article was written more than 7 years ago.

  • Hey thanks, Ed! I learned something new today!

  • @JeremyEngleWork you are welcome.

  • Instead of the IE I like it more to call a HTA to set the clipboard. It's fast and reliable and does not spam us with a commit dialog.

    Here an example script. The function 'SetClipboard' itself just creates a temporary HTA (if it's not already there, and passes the new clipboard content by command line argument.

    Set oFs = CreateObject("Scripting.FilesystemObject")

    Set oWsh = CreateObject("WScript.Shell")

    SetClipboard "Hello Clipboard"

    WScript.Echo GetClipboard()

    WScript.Quit

    Sub SetClipboard(sText)

    Dim sFile, oFile

    sFile = oWsh.ExpandEnvironmentStrings("%TEMP%")

    If sFile = "%TEMP%" Then sFile = "."

    sFile = sFile & "\~SetClipboard.hta"

    WScript.Echo sFile

    If Not oFs.FileExists(sFile) Then

    Set file = oFs.CreateTextFile(sFile, True)

    file.WriteLine "<HTA:APPLICATION id=c APPLICATIONNAME=/><SCRIPT language=VBScript>" _

    & "document.parentwindow.clipboardData.SetData ""text""," _

    & "Right(C.commandLine,Len(c.commandLine)-InStr(2,c.commandLine,Chr(34))-1):window.close" _

    & "</SCRIPT></HTA:APPLICATION>"

    file.Close : Set file = Nothing

    End If

    oWsh.Run sFile & " " & sText, 0, True

    End Sub

    Function GetClipboard()

    GetClipboard = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")

    End Function

  • Awesome1