使用VBScript中的剪贴板

使用VBScript中的剪贴板,vbscript,windows-xp,clipboard,Vbscript,Windows Xp,Clipboard,我正在寻找一种方法,把一些文字到剪贴板与。所讨论的VBScript将作为登录脚本的一部分进行部署。我希望避免使用干净的Windows XP系统上不可用的任何东西 编辑: 回答关于这是干什么的问题 我们希望鼓励组织内的用户使用文件服务器传输文档,而不是通过电子邮件不断发送附件。其中一个最大的障碍是,人们并不总是清楚什么是文件/文件夹的正确网络路径。我们开发了一个快速脚本,并将其附加到Windows上下文菜单中,这样用户就可以右键单击任何文件/文件夹,并获得一个URL,他们可以通过电子邮件发送给我们

我正在寻找一种方法,把一些文字到剪贴板与。所讨论的VBScript将作为登录脚本的一部分进行部署。我希望避免使用干净的Windows XP系统上不可用的任何东西

编辑: 回答关于这是干什么的问题

我们希望鼓励组织内的用户使用文件服务器传输文档,而不是通过电子邮件不断发送附件。其中一个最大的障碍是,人们并不总是清楚什么是文件/文件夹的正确网络路径。我们开发了一个快速脚本,并将其附加到Windows上下文菜单中,这样用户就可以右键单击任何文件/文件夹,并获得一个URL,他们可以通过电子邮件发送给我们组织中的某个人

我希望对话框中显示的URL也被放到剪贴板上

Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("clip")
Set oIn = oExec.stdIn
oIn.WriteLine "Something One"
oIn.WriteLine "Something Two"
oIn.WriteLine "Something Three"
oIn.Close

到目前为止,我找到的最接近的解决方案是使用IE在剪贴板上获取和设置内容的方法。此解决方案的问题是用户会收到安全警告。我很想把“about:blank”移动到本地计算机安全区,这样我就不会收到警告,但我不确定这会带来什么安全影响

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
objIE.document.parentwindow.clipboardData.SetData "text", "Hello This Is A Test"
objIE.Quit

我发现的另一个解决方案在我看来并不完美,但没有恼人的安全警告,那就是使用w2k3服务器上的clip.exe

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE
多行字符串示例,如以下问题所示:


为了避免与Internet Explorer和剪贴板访问相关的安全警告,我建议您使用Word应用程序对象及其方法将数据放入剪贴板。当然,您只能在安装了MS Word的机器上使用此功能,但现在大多数都是这样。(*尽管您要求使用“干净”系统:)*)


您可以在此处找到有关MS Word应用程序对象及其方法的详细信息:

Microsoft不允许VBScript直接访问剪贴板。如果您在网站上搜索“剪贴板”,您将看到:

尽管Visual Basic for Applications支持屏幕、打印机、应用程序、调试、错误和剪贴板对象,但VBScript仅支持错误对象。因此,VBScript不允许您访问鼠标指针或剪贴板等有用对象。但是,您可以使用Err对象为应用程序提供运行时错误处理


因此,间接使用记事本可能是使用VBScript所能做到的最好的方法。

如果只是文本,您不能创建一个文本文件,并在需要时读取其中的内容吗


另一种选择,显然是一个难题,就是使用
SendKeys()
方法。

这里是使用“clip”命令的另一个版本,它避免在字符串末尾添加回车符和换行符:

strA= "some character string"



我只在XP中测试过这个。clip.exe已从下载并放置在
C:\

无安全警告,完全允许和获取访问:

'create a clipboard thing
 Dim ClipBoard
 Set Clipboard = New cClipBoard

 ClipBoard.Clear  
 ClipBoard.Data = "Test"

Class cClipBoard
        Private objHTML

                Private Sub Class_Initialize
                        Set objHTML = CreateObject("htmlfile")
                End Sub

                Public Sub Clear()
                        objHTML.ParentWindow.ClipboardData.ClearData()
                End Sub

                Public Property Let Data(Value)
                        objHTML.ParentWindow.ClipboardData.SetData "Text" , Value
                End Property

                Public Property Get Data()
                        Data = objHTML.ParentWindow.ClipboardData.GetData("Text")
                End Property

                Private Sub Class_Terminate
                        Set objHTML = Nothing
                End Sub

End Class
示例用法

' Create scripting object
Dim WShell, lRunUninstall
Set WShell = CreateObject("WScript.Shell")
WShell.sendkeys "^c"
WScript.Sleep 250
bWindowFound = WShell.AppActivate("Microsoft Excel")
 WShell.sendkeys ClipBoard.Data

使用Microsoft的clip.exe最接近于拥有干净的Windows XP系统解决方案。但是,您不必调用CMD.EXE来托管它来使用它。您可以直接调用它,并在脚本代码中写入它的输入流。关闭输入流后,clip.exe将直接将内容写入剪贴板

Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("clip")
Set oIn = oExec.stdIn
oIn.WriteLine "Something One"
oIn.WriteLine "Something Two"
oIn.WriteLine "Something Three"
oIn.Close
如果需要等待剪辑完成后脚本才能继续处理,请添加

' loop until we're finished working.
Do While oExec.Status = 0
    WScript.Sleep 100
Loop
别忘了释放你的物品

Set oIn = Nothing
Set oExec = Nothing

看一看。它描述了一种从剪贴板读取的黑客方法,但我想它也可以被修改为写入剪贴板,例如将Ctrl+V更改为Ctrl+a,然后再更改为Ctrl+C。

我设计了另一种使用IE的方法,同时避免了安全警告

顺便说一下。。此函数是用JavaScript编写的。。但是你可以很容易地把它转换成VBScript

function CopyText(sTxt) {
    var oIe = WScript.CreateObject('InternetExplorer.Application');
    oIe.silent = true;
    oIe.Navigate('about:blank');
    while(oIe.ReadyState!=4) WScript.Sleep(20);
    while(oIe.document.readyState!='complete') WSript.Sleep(20);
    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>";
    var oTb = oIe.document.getElementById('txtArea');
    oTb.value = sTxt;
    oTb.select();
    oTb = null;
    oIe.ExecWB(12,0);
    oIe.Quit();
    oIe = null;
}
函数CopyText(sTxt){
var oIe=WScript.CreateObject('InternetExplorer.Application');
沉默=真实;
oIe.Navigate('about:blank');
while(oIe.ReadyState!=4)WScript.Sleep(20);
while(oIe.document.readyState!='complete')wscript.Sleep(20);
oIe.document.body.innerHTML=“”;
var oTb=oIe.document.getElementById('txtArea');
oTb.value=sTxt;
oTb.select();
oTb=null;
oIe.ExecWB(12,0);
爱。退出();
oIe=null;
}

以下是Srikanth的方法转换为vbs

function SetClipBoard(sTxt)
    Set oIe = WScript.CreateObject("InternetExplorer.Application")
    oIe.silent = true
    oIe.Navigate("about:blank")
    do while oIe.ReadyState <> 4
        WScript.Sleep 20
    loop

    do while oIe.document.readyState <> "complete"
        WScript.Sleep 20
    loop

    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>"
    set oTb = oIe.document.getElementById("txtArea")
    oTb.value = sTxt
    oTb.select
    set oTb = nothing
    oIe.ExecWB 12,0
    oIe.Quit
    Set oIe = nothing
End function


function GetClipBoard()
    set oIe = WScript.CreateObject("InternetExplorer.Application")
    oIe.silent = true
    oIe.Navigate("about:blank")
    do while oIe.ReadyState <> 4
        WScript.Sleep 20
    loop

    do while oIe.document.readyState <> "complete"
        WScript.Sleep 20
    loop 

    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>"
    set oTb = oIe.document.getElementById("txtArea")
    oTb.focus   
    oIe.ExecWB 13,0
    GetClipBoard = oTb.value
    oTb.select
    set oTb = nothing
    oIe.Quit
    Set oIe = nothing
End function
函数集剪贴板(sTxt)
Set oIe=WScript.CreateObject(“InternetExplorer.Application”)
沉默=真实
oIe.导航(“关于:空白”)
当你准备好的时候去做
WScript.Sleep 20
环
在oIe.document.readyState“完成”时执行
WScript.Sleep 20
环
oIe.document.body.innerHTML=“”
设置oTb=oIe.document.getElementById(“txtArea”)
oTb.value=sTxt
选择
设置oTb=无
oIe.ExecWB 12,0
好的,退出
设置oIe=无
端函数
函数GetClipBoard()
set oIe=WScript.CreateObject(“InternetExplorer.Application”)
沉默=真实
oIe.导航(“关于:空白”)
当你准备好的时候去做
WScript.Sleep 20
环
在oIe.document.readyState“完成”时执行
WScript.Sleep 20
环
oIe.document.body.innerHTML=“”
设置oTb=oIe.document.getElementById(“txtArea”)
焦点
oIe.ExecWB 13,0
GetClipBoard=oTb.value
选择
设置oTb=无
好的,退出
设置oIe=无
端函数

在类
剪贴板中,Clear子项和Let Data子项都不起作用。我的意思是它们对Windows
剪贴板没有影响。事实上,具有讽刺意味的是,唯一有效的子类是示例中未包含的子类,即Get Data!(我已经多次测试了这段代码。)

然而,这不是你的错。我试图用ClipboardData.SetData将数据复制到剪贴板,但这是不可能的。
Set oIn = Nothing
Set oExec = Nothing
function CopyText(sTxt) {
    var oIe = WScript.CreateObject('InternetExplorer.Application');
    oIe.silent = true;
    oIe.Navigate('about:blank');
    while(oIe.ReadyState!=4) WScript.Sleep(20);
    while(oIe.document.readyState!='complete') WSript.Sleep(20);
    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>";
    var oTb = oIe.document.getElementById('txtArea');
    oTb.value = sTxt;
    oTb.select();
    oTb = null;
    oIe.ExecWB(12,0);
    oIe.Quit();
    oIe = null;
}
function SetClipBoard(sTxt)
    Set oIe = WScript.CreateObject("InternetExplorer.Application")
    oIe.silent = true
    oIe.Navigate("about:blank")
    do while oIe.ReadyState <> 4
        WScript.Sleep 20
    loop

    do while oIe.document.readyState <> "complete"
        WScript.Sleep 20
    loop

    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>"
    set oTb = oIe.document.getElementById("txtArea")
    oTb.value = sTxt
    oTb.select
    set oTb = nothing
    oIe.ExecWB 12,0
    oIe.Quit
    Set oIe = nothing
End function


function GetClipBoard()
    set oIe = WScript.CreateObject("InternetExplorer.Application")
    oIe.silent = true
    oIe.Navigate("about:blank")
    do while oIe.ReadyState <> 4
        WScript.Sleep 20
    loop

    do while oIe.document.readyState <> "complete"
        WScript.Sleep 20
    loop 

    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>"
    set oTb = oIe.document.getElementById("txtArea")
    oTb.focus   
    oIe.ExecWB 13,0
    GetClipBoard = oTb.value
    oTb.select
    set oTb = nothing
    oIe.Quit
    Set oIe = nothing
End function
Function CopyToClipboard( sInputString )

    Dim oShell: Set oShell = CreateObject("WScript.Shell")
    Dim sTempFolder: sTempFolder = oShell.ExpandEnvironmentStrings("%TEMP%")
    Dim sFullFilePath: sFullFilePath = sTempFolder & "\" & "temp_file.txt"

    Const iForWriting = 2, bCreateFile = True
    Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    With oFSO.OpenTextFile(sFullFilePath, iForWriting, bCreateFile)
        .Write sInputString
        .Close
    End With

    Const iHideWindow = 0, bWaitOnReturnTrue = True
    Dim sCommand: sCommand = "CMD /C TYPE " & sFullFilePath & "|CLIP"
    oShell.Run sCommand, iHideWindow, bWaitOnReturnTrue

    Set oShell = Nothing
    Set oFSO = Nothing

End Function

Sub Main

    Call CopyToClipboard( "Text1" & vbNewLine & "Text2" )

End Sub

Call Main
sText = "Text Content"
CreateObject("WScript.Shell").Run "mshta.exe ""javascript:clipboardData.setData('text','" & Replace(Replace(sText, "\", "\\"), "'", "\'") & "');close();""", 0, True
sText = "Text Content and double quote "" char"
CreateObject("WScript.Shell").Run "mshta.exe ""javascript:clipboardData.setData('text','" & Replace(Replace(Replace(sText, "\", "\\"), """", """"""), "'", "\'") & "'.replace('""""',String.fromCharCode(34)));close();""", 0, True
' value to put in Clipboard
mavaleur = "YEAH"

' current Dir
path = WScript.ScriptFullName
GetPath = Left(path, InStrRev(path, "\"))

' Put the value in a file
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile=GetPath & "fichier.valeur"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write mavaleur
objFile.Close

' Put the file in the Clipboard
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c clip < " & outFile, 0, TRUE

' Erase the file
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile outFile