# Clipboard, ClipboardAll, and OnClipboardChange

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. In 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. Retrieval is reattempted according to #ClipboardTimeOut (rather than being attempted only once).

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

StringLen or StrLen 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:

- 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

A label named OnClipboardChange (if it exists) is launched automatically whenever any application (even the script itself) has changed the contents of the clipboard. The label also runs once when the script first starts.

The built-in variable A_EventInfo contains:
0 if the clipboard is now empty;
1 if it contains something that can be expressed as text (this includes files copied from an Explorer window);
2 if it contains something entirely non-text such as a picture.

The following example is a working script. Whenever it is running, it will briefly display a ToolTip for each clipboard change.

    #Persistent
    return

    OnClipboardChange:
    ToolTip Clipboard data type: %A_EventInfo%
    Sleep 1000
    ToolTip  ; Turn off the tip.
    return

If the clipboard changes while the OnClipboardChange label is already running, that notification event is lost. If this is undesirable, specify Critical as the label's first line. However, this will also buffer/defer other threads (such as the press of a hotkey) that occur while the OnClipboardChange thread is running.

If the script itself changes the clipboard, its OnClipboardChange label is typically not executed immediately; that is, commands immediately below the command that changed the clipboard are likely to execute beforehand. To force the label to execute immediately, use a short delay such as Sleep 20 after changing the clipboard.

Related topics: OnExit, OnMessage(), RegisterCallback()