Domanda

Editor's note:
While this question is specifically about copying a file reference to the clipboard, its generic title led to answers about how to copy / get text.

As an Emacs user on Windows who often attaches files in mails, I have been looking for a utility to copy a file (not its contents) to the clipboard, just as windows explorer does on righclick/copy).

I just found this right here on SO which uses System.Windows.Forms.Clipboard` in a small program to do exactly that. But it is in C#, for which I don't have immediate access to a compiler. So I am wondering if this can be done and how.

I saw several references such as this that the clipboard is not accessible in VBScripting, but msdn shows documentation for VB so I am risking the question.

I have never written a VBScript before but I did try a few things before asking, starting with running a copy pasted a "Hello world" and then various combinations of CreateObject etc.

Update: I need to call Clipboard.SetFileDropList, so I do not think I can use ClipboardData as suggested by the answers, it does not have this method.

Update for visitors

The solution I ended up using was to compile the C# itself, I did not know I already had a compiler.

Another update for visitors https://stackoverflow.com/a/29963268/18573 is what I am now using, quite happily.

È stato utile?

Soluzione 2

VBScript doesn't support the clipboard. Most hosts that host vbscript, such as Internet Explorer give access through the host. Therefore vbscript running in IE or an HTA can use IE's clipboard support. The scripting hosts do not give clipboard support. You can use a vbs file to start IE through COM automation, navigate to a local page (to bypass security warnings), then use IE's clipboard.

Here's a code snippit (Outp. is a text stream)

    Set ie = CreateObject("InternetExplorer.Application") 
ie.Visible = 0
ie.Navigate2 "C:\Users\David Candy\Desktop\Filter.html"
Do 
    wscript.sleep 100
Loop until ie.document.readystate = "complete"  
txt=ie.document.parentwindow.clipboardData.GetData("TEXT")
ie.quit
If IsNull(txt) = true then 
    outp.writeline "No text on clipboard"
else
    outp.writeline txt
End If

Altri suggerimenti

You can do it with an html object to retrieve the contents of the clipboard:

' Get clipboard text
Set objHTML = CreateObject("htmlfile")
text = objHTML.ParentWindow.ClipboardData.GetData("text")

EDIT: I use this snippet to put text back on the clipboard, but it needs third party software; a standalone executable 'clip.exe' which can be found on Windows 2003 Server or just on the internet:

' Do something with the text
text = replace(text, "you ", "you and your dog ")

' Put it back to the clipboard
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("clip")

Set oIn = oExec.stdIn

oIn.WriteLine text
oIn.Close

(Yes, it is all a little bit hackerdyhack)

You need this function (is a little modification of this):

'TO CLEAR
ClipBoard("")

'TO SET
ClipBoard("Hello World!")

'TO GET
Result = ClipBoard(Null)

Function ClipBoard(input)
'@description: A quick way to set and get your clipboard.
'@author: Jeremy England (SimplyCoded)
  If IsNull(input) Then
    ClipBoard = CreateObject("HTMLFile").parentWindow.clipboardData.getData("Text")
    If IsNull(ClipBoard) Then ClipBoard = ""
  Else
    CreateObject("WScript.Shell").Run _
      "mshta.exe javascript:eval(""document.parentWindow.clipboardData.setData('text','" _
      & Replace(Replace(Replace(input, "'", "\\u0027"), """","\\u0022"),Chr(13),"\\r\\n") & "');window.close()"")", _
      0,True
  End If
End Function

For the equivalent of a "paste" operation I would run a command-line utility like ClipOut or paste, redirect output to a file and read the file contents.

return = WshShell.Run("cmd /c clipout.exe > output.txt", 0, true)

Set fso  = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("output.txt", 1)
text = file.ReadAll
file.Close

You can get ClipOut here: http://jasonfaulkner.com/ClipOut.aspx

You can get paste here: https://www.c3scripts.com/tutorials/msdos/paste.html

For the equivalent of a "copy" operation I would use the clip command line utility that actually comes with Windows and similar code as above.

About the clip utility: https://blogs.msdn.microsoft.com/oldnewthing/20091110-00/?p=16093

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top