Clipboard and ClipboardAll

AutoHotkey GUI

Clipboard and ClipboardAll

Clipboard is a built-in variable that reflects the current contents of the Windows clipboard if those contents can be expressed as text. By contrast, ClipboardAll contains everything on the clipboard, such as pictures and formatting.

Each line of text on Clipboard typically ends with carriage return and linefeed (CR+LF), which can be expressed in the script as `r`n. Files (such as those copied from an open Explorer window via Control+C) are considered to be text: They are automatically converted to their filenames (with full path) whenever Clipboard is referenced in the script. To extract the files one by one, follow this example:

Loop, parse, clipboard, `n, `r
{
    MsgBox, 4, , File number %A_Index% is %A_LoopField%.`n`nContinue?
    IfMsgBox, No, break
}

To arrange the filenames in alphabetical order, use the Sort command. To write the filenames on the clipboard to a file, use FileAppend, %clipboard%`r`n, C:\My File.txt. To change how long the script will keep trying to open the clipboard -- such as when it is in use by another application -- use #ClipboardTimeout.

Basic examples:
clipboard = my text   ; Give the clipboard entirely new contents.
clipboard =   ; Empty the clipboard.
clipboard = %clipboard%   ; Convert any copied files, HTML, or other formatted text to plain text.
clipboard = %clipboard% Text to append.   ; Append some text to the clipboard.
StringReplace, clipboard, clipboard, ABC, DEF, All   ; Replace all occurrences of ABC with DEF (also converts the clipboard to plain text).

Using ClipWait to improve script reliability:

clipboard =  ; Start off empty to allow ClipWait to detect when the text has arrived.
Send ^c
ClipWait  ; Wait for the clipboard to contain text.
MsgBox Control-C copied the following contents to the clipboard:`n`n%clipboard%

ClipboardAll (saving and restoring everything on the clipboard)

ClipboardAll contains everything on the clipboard (such as pictures and formatting). It is most commonly used to save the clipboard's contents so that the script can temporarily use the clipboard for an operation. When the operation is completed, the script restores the original clipboard contents as shown below:

ClipSaved := ClipboardAll   ; Save the entire clipboard to a variable of your choice.
; ... here make temporary use of the clipboard, such as for pasting Unicode text via Transform Unicode ...
Clipboard := ClipSaved   ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved =   ; Free the memory in case the clipboard was very large.

ClipboardAll may also be saved to a file (in this mode, FileAppend always overwrites any existing file):

FileAppend, %ClipboardAll%, C:\Company Logo.clip ; The file extension does not matter.

To later load the file back onto the clipboard (or into a variable), follow this example:

FileRead, Clipboard, *c C:\Company Logo.clip ; Note the use of *c, which must precede the filename.

Notes

ClipboardAll is blank when used in ways other than those described above. In addition, ClipboardAll is not supported inside comma-separated expressions; that is, it should be assigned on a line by itself such as ClipSaved := ClipboardAll.

Variables to which ClipboardAll has been assigned are in binary format and thus will appear as gibberish when displayed with MsgBox or similar. Also, altering a binary-clipboard variable (by means such as StringReplace) will revert it to a normal variable, resulting in the loss of its clipboard data. [v1.0.46+]: Binary-clipboard variables may be passed to functions by value (formerly they only worked ByRef).

If ClipboardAll cannot retrieve one or more of the data objects (formats) on the clipboard, they will be omitted but all the remaining objects will be stored.

A variable containing clipboard data can be copied to another variable as in this example: ClipSaved2 := ClipSaved.

ClipWait may be used to detect when the clipboard contains data (optionally including non-text data).

StrLen / StringLen may be used to discover the total size of a variable to which ClipboardAll has been assigned. However, to get the size in bytes on Unicode versions of AutoHotkey, the length must be multiplied by 2. A_IsUnicode can be used to support ANSI and Unicode versions, as in this example: size := StrLen(ClipSaved) * (A_IsUnicode ? 2 : 1).

Variables to which ClipboardAll has been assigned can be compared to each other (but not directly to ClipboardAll) by means of the <> and = operators. In the following example, the length of each variable is checked first. If that is not enough to make the determination, the contents are compared to break the tie:

if ClipSaved1 <> %ClipSaved2%   ; This must be an old-style IF statement, not an expression.
    MsgBox The two saved clipboards are different.

Saving ClipboardAll to a variable is not restricted by the memory limit set by #MaxMem.

A saved clipboard file internally consists of a four-byte format type, followed by a four-byte data-block size, followed by the data-block for that format. If the clipboard contained more than one format (which is almost always the case), these three items are repeated until all the formats are included. The file ends with a four-byte format type of 0.

Known limitation: Retrieving ClipboardAll while cells from Microsoft Excel are on the clipboard may cause Excel to display a "no printers" dialog.

Clipboard utilities written in AutoHotkey:

OnClipboardChange

Scripts can detect changes to the content of the Clipboard by using OnClipboardChange.