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 with Control-C) are considered to be text: They are auto-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 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.

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) [v1.0.29+]

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 complete, 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, 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 variable -- by means such as StringReplace -- will revert it to a normal variable, resulting in the loss of its clipboard data.

If ClipboardAll cannot retrieve one or more of the data objects 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).

StringLen may be used to discover the total size of a variable to which ClipboardAll has been assigned.

In v1.0.30+, 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.

 

OnClipboardChange [v1.0.35.10+]

A label named OnClipboardChange 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;
2 if it contains something entirely non-text such as a picture.

In versions prior to 1.0.36, ErrorLevel must be used instead of A_EventInfo. In later versions, both of these variables will initially contain the same thing, but A_EventInfo is preferable because it is not altered by commands that change ErrorLevel.

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

Related topics: OnExit, OnMessage()