Clipboard

Clipboard is a built-in variable that reflects the current contents of the Windows clipboard if those contents can be expressed as text.

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

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

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

Related

Examples

#1

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.
Clipboard := StrReplace(Clipboard, "ABC", "DEF")   ; Replace all occurrences of ABC with DEF (also converts the clipboard to plain text).

Clipboard utilities written in AutoHotkey v1: