# 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.

### Limitations

Some limitations apply to the direct use of ClipboardAll:

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

A variable containing binary clipboard data can be used as follows:

- Put the data back on the clipboard by assigning to Clipboard as shown above.
- Write the clipboard data to file with FileAppend, as shown above.
- Copy it to another variable (or ByRef parameter) as in this example: `ClipSaved2 := ClipSaved`.
- Pass it to a user-defined function by reference, or in [v1.0.46+], by value.
- Determine the size of the data by passing it to StrLen() / StringLen as described below.
- Compare two such variables by using an old-style IF as shown below.
- Take its address or pass the variable itself to NumGet to examine the data.

Further limitations apply:

- When binary clipboard data is assigned to a variable, the variable is marked with a hidden attribute. This attribute can only be set by FileRead with the `*c` option, or by direct assignment from ClipboardAll (as shown above) or another variable which has the attribute.
- When used in ways other than those described above, binary clipboard data is usually interpreted as text and truncated at the first null character, which is typically at the beginning of the data. For instance, this occurs when one attempts to return the data from a function or assign it to a property or array element.
- The clipboard attribute is removed when the variable is freed or assigned any other value, even by indirect means such as StringReplace.
- Variables without the clipboard attribute are treated as text when assigned to Clipboard or passed to FileAppend, even if the data is in the appropriate binary format.

### Notes

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.

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 IfEqual, IfNotEqual, `If Var1 = %Var2%`, or `If Var1 != %Var2%`. 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 (for 32-bit) or eight-byte (for 64-bit) 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:

- Deluxe Clipboard: Provides unlimited number of private, named clipboards to Copy, Cut, Paste, Append or CutAppend of selected text. www.autohotkey.com/forum/topic2665.html
- ClipStep: Control multiple clipboards using only the keyboard's `Ctrl`-`X`-`C`-`V`. www.autohotkey.com/forum/topic4836.html

## OnClipboardChange

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