AutoHotKey将文件复制到剪贴板并粘贴

时间:2017-05-15 15:58:24

标签: load clipboard autohotkey paste

我正在尝试使用AutoHotKey来自动完成我的一些工作。当与客户对应时,我有一个我使用的模板。我已经创建了模板并将其复制到剪贴板并使用AutoHotKey脚本将其保存到文件中 - 这部分工作正常。我现在想要保存剪贴板中的任何内容,加载预先保存的文件,将其粘贴到我的Outlook消息窗口中,然后将保存的剪贴板恢复到剪贴板。我已经尝试了几种方法但没有成功 - 通常,复制到Outlook的内容是最初在剪贴板中的内容。以下是我尝试过的脚本:

^F5::
  ClipSaved := ClipboardAll ; Save the entire clipboard to ClipSaved (Not just text)
  Clipboard =          ; Clear the clipboard
  FileRead, Clipboard, *c <fullpath to saved file like c:\dir\file.clip>
  MyErr = %ErrorLevel%
  if MyErr >= 1
  {
      MsgBox, Unable to read case_format.clip!
  }
  ClipWait, 5
  SendInput, ^v
  Clipboard := ClipSaved        ; Restore the clipboard we saved
  ClipSaved =                   ; Free the memory in case the clipboard was very large
Return

我也尝试过使用WinClipApi,它也无法正常工作。它没有复制任何内容,我最终得到了来自Windows的“哔”:

^F5::
    WinClip.Snap( ClipSaved )
    WinClip.Clear()
    WinClip.Load( <full path to file like "c:\dir\file.clip"> )
    ClipWait,5,
    WinClip.Paste()
    WinClip.Restore( ClipSaved )
Return

我尝试过混合和匹配的部分(比如在WinClip示例中使用FileRead AHK命令,将'SendInput,^ v替换为WinClip.Paste()等等)但似乎没有任何效果。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可能在Windows处理 ctrl + v

之前销毁剪贴板

在修改剪贴板之前,等待窗口完成粘贴

SendInput, ^v
Sleep 2000                   ; Wait 2s for Windows to finish with clipboard
Clipboard := ClipSaved 

延迟的长度取决于您粘贴的内容。粘贴100mb图像需要比几行文本更长的延迟

如果剪贴板包含文本以外的内容,请考虑使用clipwait的第二个参数:

ClipWait, 2, 1