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() returns an object containing 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
{
    Result := MsgBox("File number %A_Index% is %A_LoopField%.`n`nContinue?",, 4)
    if Result = "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.
StrReplace, clipboard, %clipboard%, ABC, DEF   ; 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)

Creates an object containing everything on the clipboard (such as pictures and formatting).

ClipSaved := ClipboardAll(Data, Size)

Parameters

Omit both parameters to retrieve the current contents of the clipboard. Otherwise, specify one or both parameters to create an object containing the given binary clipboard data.

Data

A string containing binary data, or a pure integer which is the address of the binary data. The data must be in a specific format, so typically originates from a previous call to ClipboardAll(). See FileAppend below.

Size

The number of bytes of data to use. This is optional when Data is a string.

ClipboardAll Object

The return value is a ClipboardAll object, which has three properties:

Data

A string containing raw binary data which represents the clipboard contents. This is typically passed to FileAppend or File.RawWrite to write it to file.

Ptr

The address of the data contained by the object. This address is valid until the object is freed.

Size

The size, in bytes, of the raw binary data.

Saving and Restoring 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 quickly pasting large amounts of text ...
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:

; Option 1: Delete any existing file and then use FileAppend in "RAW" mode.
FileDelete "C:\Company Logo.clip"
FileAppend ClipboardAll().Data, "C:\Company Logo.clip", "RAW" ; The file extension does not matter.

; Option 2: Use FileOpen in overwrite mode and File.RawWrite.
FileOpen("C:\Company Logo.clip", "w").RawWrite(ClipboardAll().Data)

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

Clipboard := ClipboardAll(FileRead("C:\Company Logo.clip", "RAW"))

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.

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

Binary data returned by the Data property 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 data 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 v1:

OnClipboardChange

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